asp.net 动态添加多个用户控件
程序员文章站
2024-03-31 20:04:22
用户控件代码: 代码webcontrols 复制代码 代码如下: using system; using system.collections.generic; using...
用户控件代码:
代码webcontrols
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
namespace xuyuanwang.mycontrol
{
public partial class lablexuyuan : system.web.ui.usercontrol
{
string a = "ok";
public string a
{
set
{
a = value;
}
get
{
return a;
}
}
protected void page_load(object sender, eventargs e)
{
label1.text = a;
}
}
}
aspx页面代码:
代码
public partial class webform1 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void button1_click(object sender, eventargs e)
{
mycontrol.lablexuyuan ctl = (mycontrol.lablexuyuan)page.loadcontrol("mycontrol/lablexuyuan.ascx");
ctl.a = this.textbox1.text;
arraylist list = addl(ctl);
for (int i = 0; i < list.count; i++)
{
mycontrol.lablexuyuan ctl2 = (mycontrol.lablexuyuan)list[i];
this.updatepanel1.contenttemplatecontainer.controls.add(ctl2);
}
}
private system.collections.arraylist addl(mycontrol.lablexuyuan l)
{
system.collections.arraylist list = null;
if (session["a"] != null)
{
list = (arraylist)session["a"];
}
else
{
list = new arraylist();
}
list.add(l);
session["a"] = list;
return list;
}
代码webcontrols
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
namespace xuyuanwang.mycontrol
{
public partial class lablexuyuan : system.web.ui.usercontrol
{
string a = "ok";
public string a
{
set
{
a = value;
}
get
{
return a;
}
}
protected void page_load(object sender, eventargs e)
{
label1.text = a;
}
}
}
aspx页面代码:
代码
复制代码 代码如下:
public partial class webform1 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void button1_click(object sender, eventargs e)
{
mycontrol.lablexuyuan ctl = (mycontrol.lablexuyuan)page.loadcontrol("mycontrol/lablexuyuan.ascx");
ctl.a = this.textbox1.text;
arraylist list = addl(ctl);
for (int i = 0; i < list.count; i++)
{
mycontrol.lablexuyuan ctl2 = (mycontrol.lablexuyuan)list[i];
this.updatepanel1.contenttemplatecontainer.controls.add(ctl2);
}
}
private system.collections.arraylist addl(mycontrol.lablexuyuan l)
{
system.collections.arraylist list = null;
if (session["a"] != null)
{
list = (arraylist)session["a"];
}
else
{
list = new arraylist();
}
list.add(l);
session["a"] = list;
return list;
}