[C #] [sample] dynamic loading control [placeholder control]

Source: Internet
Author: User
[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.