ASP. NET Control ID
It is often said that in ASP. NET does not use dynamic controls. I think the main reason is that using dynamic controls can cause some problems, 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, 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?
The usage is as follows:
- Document. all.<% = TestControl. ClientID %>. Disabled=True;
- // Set TestControl to unavailable
-
- 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 ASP. NET 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, ASP. NET Control ID is changed to user control ID _ child Control ID. When a control is dynamically loaded, if the control ID is not assigned a value, ASP. the. NET 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.
- Analysis of Theme functions in ASP. NET development skills
- ASP. NET Dynamic Compilation
- Analysis on ASP. NET supported by Apache
- Introduction to ASP. NET Server standard controls
- Analysis on SQL Server Database Backup Recovery in ASP. NET