Introduction to ASP. NET Control ID

Source: Internet
Author: User

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

 
 
  1. Control userControl=(Control)Page.LoadControl(“Test.ascx”);  
  2. userControl.ID=“Test”;  
  3. 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:

 
 
  1. 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 ):

 
 
  1. public class BaseControl : System.Web.UI.UserControl  
  2. {  
  3. public virtual BaseControl ProcessThisControl();  
  4. }  

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:

 
 
  1. BaseControl userControl=(BaseControl )Page.LoadControl(“Test.ascx”);  
  2. 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:

 
 
  1. Document. all.<% = TestControl. ClientID %>. Disabled=True;
  2. // Set TestControl to unavailable
  3.  
  4. Page. RegisterStartupScript ("OnInitControl ","<SCRIPT LANGUAGE='Javascript'>
    Document. all. Test_TestControl.disabled=True;</SCRIPT>");
  5. // 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.

  1. Analysis of Theme functions in ASP. NET development skills
  2. ASP. NET Dynamic Compilation
  3. Analysis on ASP. NET supported by Apache
  4. Introduction to ASP. NET Server standard controls
  5. Analysis on SQL Server Database Backup Recovery in ASP. NET

Related Article

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.