4.placeholder 容器控件,不会产生任何客户端代码
textbox等集合类的使用:
public class textboxArray:system.collection.collectionbase
{
private readonly system.web.control hostpage;
public textboxArray(system.web.control host)
{
hostpage = host;
}
public void AddNewTextBox()
{
TextBox atextbox = new TextBox();
this.List.Add(atextbox);
hostpage.controls.add(atextbox);
atextbox.style["left"] = Convert.ToString((count-1)*130+70);
atextbox.style["top"] = Convert.ToString(160);
atextbox.width = 120;
atextbox.borderstyle = boderstyle.outset;
atextbox.id = this.count.ToString();
atextbox.text = "textbox示例";
atextbox.forecolor = sytem.drawing.color.blue;
}
//创建索引
public TextBox this[int index]
{
get
{
return (TextBox)this.List[index];
}
}
//remove方法来删除控件
public void remove()
{
if(this.count>0)
{
hostpage.controls.remove(this[this.count-1]);
this.List.removeAt(this.count-1);
}
}
}
5.添加控件事件:
Button btn1 = new Button("click me");
btn1.CommandArgument = "btn1";
btn1.Command += new CommandEventHandler(this.OnButton);
ph.controls.add(btn1);
下面事件函数:
public void OnButton(object sender,CommandEventArgs e)
{
---------normal
}
Control c3 = ParseControl("<asp:button id='btn2' text='click btn2' CommandName='btn' CommangArgument='b2' runat='server'></asp:button>");
Button btn3 = (Button)page.FindControl("btn2");
btn3.Command += new CommandEventHandler(this.OnButton);
事件函数变为:
public void OnButton(object sender,CommandEventArgs e)
{
switch(e.CommandArgument.Tostring().toLower())
{
case "btn1":
case "b2":
}
}