Summary of some problems when loading controls dynamically in Asp.net

Source: Internet
Author: User
It is often said that do not use dynamic controls in Asp.net. I think the main reason is that using dynamic controls will cause some problems. During the project process, I will always make a small summary of what is triggered by dynamic loading controls.
1. After loadcontrol is used to load the control, some controls in the user control no longer respond to events.
This problem is mainly caused by loading the control in if (! Page. ispostback. This issue is described in detail on the Si GUI blog.
2. A problem occurs in the response of some controls in the user control. For example, when a button is selected for the first time, the click event is not triggered, and the second time is OK.
This is because the ID is not set for the control. The role of the Control ID is described in detail below. For example
Control usercontrol = (Control) page. loadcontrol ("test. ascx ");
Usercontrol. ID = "test ";
Addcontrol (usercontrol );
3. If the user control contains the DataGrid control, the control may not respond to the DataGrid event after it is loaded.
This seems to be a bug. You must forcibly convert the loaded control, for example:
Test usercontrol = (TEST) page. loadcontrol ("test. ascx ");
Note: The test type is used above, rather than control!
I mentioned this in my previous blog. This method will reduce the scalability of the system. I have a solution to discuss with you (using the Policy Model ):
Public class basecontrol: system. Web. UI. usercontrol
{
Public Virtual basecontrol processthiscontrol ();

}
All user controls are inherited from basecontrol. If a DataGrid control exists, the overide processthiscontrol method is used, for example:
Return this as test;
Load the control as follows:
Basecontrol usercontrol = (basecontrol) page. loadcontrol ("test. ascx ");
Usercontrol. processthiscontrol ();

4. How to use JavaScript in user controls.
As we all know, using client scripts will greatly increase the page response speed and avoid frequent page refreshing. Therefore, using JavaScript to implement partial page control is a good method, but what if a subcontrol is accessed in a user control?

Use the following method: Document. All. <% = testcontrol. clientid %>. Disabled = true; // set testcontrol to unavailable
In the C # script, write: page. registerstartupscript ("oninitcontrol", "<script language = 'javascript '> document. all. test_testcontrol.disabled = true; </SCRIPT> "); // test is the user control, and testcontrol is the Child control in the user control.

Now let's talk about the control ID. when accessing the aspx file, IIS will compile the aspx script. During compilation, the content of the user control is written in the same page. To prevent the control name in the page from being the same as that in the user control, the name of the control in the user control is changed: user Control name: subcontrol. The control ID is changed to the User Control ID. When a control is dynamically loaded, if the control ID is not assigned a value, the Control ID is the ID of the control that was last loaded. Therefore, you should set the Id immediately after the user control is loaded.

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.