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.


 

The ID is the server identifier of the server control. In the server code, the ID uniquely identifies the Server Control (so the ID must be unique on the same page );

 

ClientID is the Client ID after the server control is converted to an HTML element. To 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;


UniqueID is the unique identifier of the server control that is defined in hierarchical form. It is also the name identifier of the client after the server control is converted into an HTML element.


Sometimes we need to use Javascript on the client to operate HTML elements through the HTML element id. If the server control is in the data binding control, the id of the final HTML element is very different from what 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, and adds the ClientIDMode attribute for control. 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" when the client ID of the controller is no different from the previous ASP. NET;

When ClientIDMode = "Static", the client ID of the controller does not change; www.2cto.com

ClientIDMode = "Predictable" The Client ID of the 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.


 

From Xu Yue's column

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.