asp.net| Dynamic | load | control | Problems often see people say do not use dynamic controls in ASP.net, I think the main reason is that the use of dynamic control will bring some problems, in the process of doing the project, I will be triggered by dynamic loading control is always a small sum.
1. After the control is loaded with LoadControl, some controls in the user control no longer respond to events.
This problem is mainly due to the fact that the control is loaded in the IF (! Page.IsPostBack), put it on the outside. This issue is explained in detail on Shi's blog.
2, the user control some controls in the response to problems, such as a button when the first selection does not trigger the Click event, the second can be.
This is due to the fact that the control ID is not set by the ID, which is described in detail below. Such as
Control usercontrol= (Control) Page.LoadControl ("Test.ascx");
Usercontrol.id= "Test";
AddControl (UserControl);
3. If the DataGrid control is included in the user control, a problem that does not respond to the DataGrid event may occur after the control is loaded.
This appears to be a bug, and you must cast the loaded control, such as:
Test usercontrol= (Test) Page.LoadControl ("Test.ascx");
Note: The test type is used above, not control!
I have mentioned this problem in my previous blog, which will make the system less scalable. 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 inherit from Basecontrol, and if there is a DataGrid control, the Overide Processthiscontrol method, such as:
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 you all know, using the client script will greatly improve the response speed of the page, while avoiding frequent refresh of the page. So it's a good way to use JavaScript to implement partial control in a page, but what about a child control in a user control?
Use the following methods: document.all.<%= testcontrol.clientid%>.disabled=true; Set TestControl to Not available
If you should write this in a C # script: Page.registerstartupscript ("Oninitcontrol", "<script language= ' JavaScript ' >document.all.test _testcontrol.disabled=true;</script> "); Test is a user control, TestControl as a child control in the user control.
Now for the control ID, when accessing the ASPX file, IIS compiles the ASPX script. The contents of the user control are written on the same page at compile time. To prevent the control in the page from being the same as the name of the control in the user control, modify the name of the control in the user control to the user control name: The child control, and the control ID to the user Control Id_ child control ID. When a control is dynamically loaded, if the ID of the control is not assigned, the control ID is the last loaded control ID, so you should set the ID immediately after the user control is loaded.