Client name of ASP. NET Control

Source: Internet
Author: User
Tags reflector

We are using ASP. when you use a browser to view the generated client code on a web page written by. net, you often see the code gridview1_ctl10?webusercontrol=webusercontrolbutton. What is the naming rule and how to pull the code? This time we use reflector to view. Net code and study the rules.

Our ASP. NET Server-side controls generally have two attributes: ID and name when generating client controls. These two attributes can be obtained on the server side through clientid and uniqueid.
Take a button as an example. Use reflector to open the system. web. DLL, find system. web. UI. in the webcontrols namespace, we can find that this class inherits from the webcontrol class. In fact, most controls inherit from this class. This class inherits system. web. UI. control class, which is the focus of our research. This class inherits from system. object class, which is the base class of all classes, so we will not study it. Next let's take a look at the control class.
Find the clientid attribute under the control class and check its Code as follows:

Public Virtual string clientid
...{
Get
...{
This. ensureid ();
String uniqueid = This. uniqueid;
If (uniqueid! = NULL) & (uniqueid. indexof (this. idseparator)> = 0 ))
...{
Return uniqueid. Replace (this. idseparator ,'_');
}
Return uniqueid;
}
}

In other words, clientid replaces idseparator (with the value "$") with "_" in uniqueid "_". For example, the code generated by writing a page is as follows:
<Input type = "Submit" name = "gridview1 $ ctl101 $ webusercontrol1 $ webusercontrolbutton" value = "button" id = "gridview1_ctl10?webusercontrol=webusercontrolbutton"/>
Obviously, the difference between name and ID is to replace $ _.
Now we know the clientid. How is the uniqueid generated? Let's take a look at it with reflector.

Public Virtual string uniqueid
...{
Get
...{
If (this. _ cacheduniqueid = NULL)
...{
Control namingcontainer = This. namingcontainer; // obtain the parent Control
If (namingcontainer = NULL)
...{
Return this. _ id;
}
If (this. _ id = NULL)
...{
This. generateautomaticid (); // automatically number the control.
}
If (this. Page = namingcontainer) // if the parent control of the current control is page, uniqueid is the Control ID.
...{
This. _ cacheduniqueid = This. _ id;
}
Else // The current control parent control is another container control
...{
String uniqueidprefix = namingcontainer. getuniqueidprefix (); // obtain the uniqueid + delimiter ($) of the parent control as the uniqueid prefix of the current control.
If (uniqueidprefix. Length = 0)
...{
Return this. _ id;
}
This. _ cacheduniqueid = uniqueidprefix + this. _ id; // prefix + ID as the uniqueid of the Current Control
}
}
Return this. _ cacheduniqueid;
}
}

The most important part of this Code is the generateautomaticid () function and namingcontainer. getuniqueidprefix (); function. Let's take a look at how the function is implemented.

Private void generateautomaticid ()
...{
This. Flags. Set (0x200000 );
This. _ namingcontainer. ensureoccasionalfields ();
Int Index = This. _ namingcontainer. _ occasionalfields. namedcontrolsid ++;
If (this. enablelegacyrendering)
...{
This. _ id = "_ CTL" + index. tostring (numberformatinfo. invariantinfo );
}
Else if (index <0x80)
...{
This. _ id = automaticids [Index];
}
Else
...{
This. _ id = "CTL" + index. tostring (numberformatinfo. invariantinfo );
}
This. _ namingcontainer. dirtynametable ();
}

From this function, we can see that for a control bound like a gridview, the name of the control in each row generated by the control is composed of CTL + auto-increment numbers. The number is formatted with two digits, that is, when there are less than two digits, the number is set to zero. If there are two more digits, the actual content is used.

Internal virtual string getuniqueidprefix ()
...{
This. ensureoccasionalfields ();
If (this. _ occasionalfields. uniqueidprefix = NULL)
...{
String uniqueid = This. uniqueid;
If (! String. isnullorempty (uniqueid ))
...{
This. _ occasionalfields. uniqueidprefix = uniqueid + this. idseparator;
}
Else
...{
This. _ occasionalfields. uniqueidprefix = string. empty;
}
}
Return this. _ occasionalfields. uniqueidprefix;
}

This function returns the uniqueid + idseparator of the parent control. If the uniqueid of the parent control is null, null is returned.
Now let's look back at the name of gridview1 $ ctl101 $ webusercontrol1 $ webusercontrolbutton: we can see that this is a webusercontrol bound to the gridview control, and this control has a webusercontrolbutton control.

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.