asp.net| Dynamic | control | page
The beginning of the way:
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
String ctr = "<asp:textbox runat= ' server ' id= ' haha ' text= ' button '/> ';
Control Ctrl = Page.parsecontrol (CTR);
PLACEHOLDER1.CONTROLS.ADD (ctrl);
}
}
The add control succeeds, but when the client is postback, the above text will cause the newly added control to disappear. Of course, if you remove the IsPostBack control, it will not disappear, but adding controls every time the client postbacks can cause unnecessary resource wastage. How to solve it?
You need to look at the life cycle of the page. Here just talk about what we need, do not need to see for yourselves (Recommended book bar "asp.net2.0 Technology Insider", sister article "asp.net2.0 Advanced Programming")
Preinit-->init-initcomplete->load .....
PreInit occurs at the beginning of page initialization.
Init occurs when a server control initializes, and initialization is the first step in the lifetime of the control.
InitComplete occurs when the page initialization completes.
Load occurs when the server control is loaded into the Page object.
Okay, we can go on with our action.
protected void Page_Init (Object Sernder, EventArgs e)
{
String ctr = "<asp:textbox runat= ' server ' id= ' haha ' text= ' button '/> ';
Control Ctrl = Page.parsecontrol (CTR);
PLACEHOLDER1.CONTROLS.ADD (ctrl);
}
Ok.
Test it yourself.