[C #] [sample] dynamic loading control [placeholder control]
Placeholder is the ASP. NET placeholder Web Server Control in msdn, which enables you to place the empty container control on the page, and then
ADD child elements to the container dynamically during runtime. It is an empty container, so it can carry other controls, it has a similar
Function.
The following two examples are used to deepen your understanding of it.
Example 1:
1. Add a placeholder control on the page;
2. Create the user control dynamicusercontrol. ascx.
3. Add button and add user Code :
Static bool holded = true; // can be replaced by session
Protected void btnloadusercontrol_click (Object sender, eventargs E)
{
If (holded = true)
{
Control C1 = loadcontrol ("dynamicusercontrol. ascx ");
Placeholder1.controls. Add (C1 );
Holded = false;
Btnloadusercontrol. Text = "Hide user control ";
}
Else
{
Placeholder1.controls. Clear ();
Holded = true;
Btnloadusercontrol. Text = "loading user control ";
}
}
4. effect: click the button repeatedly to see the appearance and disappearance of the user control.
Example 2:
1. Add a placeholder control on the page;
2. Add the button and add the user code:
protected void btnloadusercontrol2_click (Object sender, eventargs e)
{< br> button btn1 = new button ();
btn1.text = "create after pressing the button above and display it,";
placeholder2.controls. add (btn1);
btn1.text + = "display the ID of my container is placeholder2! Commanda
if a problem occurs during the test, the IIS is not working properly.
Backup again