ASP. net4.0 controls the ID of the server control (Excerpted from Zhou Gong's blog)

Source: Internet
Author: User

 

We know that an ASP. NET Server Control has three IDs: ID, clientid, and uniqueid.

 

IDIndicates the server of the server control,Uniquely differentiate server controls by ID in server code(Therefore, the ID must be unique in the same page );

 

ClientidIs the server control being convertedThe ID of the client after the HTML ElementTo prevent name conflicts, ASP. NET automatically generates a unique clientid value for each server control on the page.The clientid value is generated by connecting the control's ID value and its parent control's uniqueid value.Each part of the generated ID is separated by an underscore;

UniqueidYesThe unique identifier of the server control that is defined in a hierarchical manner. It is also the name identifier of the server control after it is converted into an HTML element.

Sometimes we need to use JavaScript on the client to operate HTML elements through the ID of HTML elements. IfServer Control in data binding controlThe ID of the final HTML element is very different from the one we usually use, which makes it inconvenient for us to perform operations, especially when we use JavaScript to implement full data selection and invert selection, when operating a single control, we can also write JavaScript Code as follows:

 

<SCRIPT type = "text/JavaScript"> <! --
Function dosomething (){
Alert ('<% = Control. clientid %>');
}
--> </SCRIPT>

 

ASP. NET provides stronger control over the ID of the HTML element corresponding to the server control.ClientidmodeAttribute. The following is an example of the clientidmode attribute usage. The server encoding is as follows:

<Asp: gridview id = "gvuserlist" runat = "server" autogeneratecolumns = "false">
<Columns>
<Asp: templatefield headertext = "userid_autoid">
<Itemtemplate>
<Asp: Label runat = "server" id = "lB1" clientidmode = "autoid"

TEXT = '<% # eval ("userid") %>'/>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "userid_static">
<Itemtemplate>
<Asp: Label runat = "server" id = "lB1" clientidmode = "static"

TEXT = '<% # eval ("userid") %>'/>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield headertext = "userid_predictable">
<Itemtemplate>
<Asp: Label runat = "server" id = "lB1" clientidmode = "predictable"

TEXT = '<% # eval ("userid") %>'/>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>

 

If we bind the data before viewing the generated HTML source code, we will see a similar part:

<Table cellspacing = "0" rules = "all" border = "1" id = "maincontent_gvuserlist" style = "border-collapse: collapse;" mce_style = "border-collapse: collapse; ">
<Tr>
<TH scope = "col"> userid_autoid </Th> <TH scope = "col"> userid_static </Th> <TH scope = "col"> userid_predictable </Th>
</Tr>
<Tr>
<TD> <span id = "maid"> 136 </span> </TD>
<TD> <span id = "lB1"> 136 </span> </TD>
<TD> <span id = "maincontent_gvuserlist_lb1_0"> 136 </span> </TD>
</Tr>
....
</Table>

Clientidmode = "autoid"The client ID of the time controller is no different from that of ASP. NET;

Clientidmode = "static"The client ID of the time controller does not change;

Clientidmode = "predictable"The client ID of the time controller also carries the data row ID

(For example, "0" in ID = "maincontent_gvuserlist_lb1_0" indicates that the position in the data source is 0, that is, the first record ).

By specifying the clientidmode attribute, we can easily control the ID of the HTML element generated by the server-side control, which is also quite convenient.

 

Source Address: http://blog.csdn.net/zhoufoxcn/archive/2010/05/17/5599115.aspx #

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.