How to dynamically load User Controls

Source: Internet
Author: User
When a user control is dynamically loaded, some attributes and methods of some controls are called, causing confusion in the control naming.
Because when you add a user control or loadcontrol
If the control ID and clientid are not specified, the initial ID is: "_ ctl0"

If you call textbox1.clientid --> in the user control before adding the user control to the page, the user control is named.
At this time, the user control has not been added to the page, so the clientid generated at this time certainly does not have the name of the parent control.
In addition, the initial ID of the sub-control is: "_ ctl0", and then go down, as long as it is not named _ ctl1, _ ctl2 ......

In this way, when the user control is truly added to the page, the user control does not specify the Control ID, then the user control ID will be
Initial ID: "_ ctl0"

We should see the problem.
The child control and the parent control have the same ID. As a result, the viewstate of textbox1 in the user control cannot be restored, and the button event cannot be triggered.
If you first add the user control to the page and then perform the operation, the control name in the user control will be changed:

User Control
ID type
_ Ctl0 ASP. webusercontrolpolicascx
_ Ctl0: _ ctl0 system. Web. UI. literalcontrol
_ Ctl0: button2 system. Web. UI. webcontrols. Button
_ Ctl0: _ ctl1 system. Web. UI. literalcontrol
............................
This will not cause duplicate names.

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.