Home >Backend Development >C#.Net Tutorial >Summary of some problems when dynamically loading controls in ASP.net
I often see people saying not to use dynamic controls in ASP.net. I think the main reason is that using dynamic controls will bring some problems. In the process of working on projects, I always make a A small summary.
1. After using LoadControl to load controls, some controls in the user control no longer respond to events.
This problem is mainly caused by placing the control loading inside if (!Page.IsPostBack), just put it outside. This issue is explained in detail on Sigui’s blog.
2. There is a problem with the response of some controls in the user control. For example, a button does not trigger the CLICK event when it is selected for the first time, but it does OK the second time.
This is caused by not setting an ID for the control. The function 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 includes the DataGrid control, then it may be possible after loading the control There is a problem of not responding to DataGrid events.
This seems to be a bug, the loaded control must be forced to convert, such as:
Test userControl=(Test)Page.LoadControl("Test.ascx");
Note: The Test type is used above, not Control !
I mentioned this problem in my previous blog. This approach will reduce the scalability of the system. I have a solution to discuss with you (using strategy pattern):
public class BaseControl: System.Web.UI.UserControl
{
public virtualBaseControl ProcessThisControl();
}
All user controls inherit from BaseControl, if any Datagrid control, by 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.
Everyone knows that using client-side scripts will greatly improve the response speed of the page and avoid frequent page refreshes. So using JavaScript to implement partial control on the page is a better way, but what if you access a certain sub-control in a user control?
Usage as follows: document.all.<%= TestControl.ClientID%>.disabled=true; //Set TestControl to be disabled
If in C# script It should be written like this: Page.RegisterStartupScript("OnInitControl",""); //Test is a user control, TestControl As a child control in the user control.
Now let’s talk about the control ID. When accessing an aspx file, IIS will compile the aspx script. When compiling, write the content in the user control on the same page. In order to prevent the controls on the page from having the same name as the controls in the user control, change the control names in the user control to: User control name: sub control, the control ID is changed to user control ID_child control ID. When loading a control dynamically, if the ID of the control is not assigned, the control ID will be the control ID of the last loaded control, so the ID should be set immediately after the user control is loaded.