Differences between the ID, ClientID, and UniqueID of the asp.net Server Control

Source: Internet
Author: User

1. Brief Introduction
ID is the ID you specify during design. It is the programming identifier assigned to the control on the server. We often use this. controlid to find the control. Then, this controlid is the ID mentioned here.
ClientID is the Client Identifier of the server Control Generated by ASP. Net. when the control is generated on the client page, it needs to be accessed through ClientID when the client code accesses the control.
The unique, hierarchical, and qualified identifier of the UniqueID Server Control. It is used when you need to participate in the server return. When you place controls in repeated controls (Repeater, DataList, and DataGrid), multiple server-side controls may be generated, which requires distinguishing the various controls on the server, so that their ID attributes do not conflict. UniqueID is generated by connecting the UniqueID value of the parent control of the Child control with the ID value of the control. Each part is connected by the character specified by the IdSeparator attribute. By default, the IdSeparator attribute is a colon character (:). This attribute is newly added in. Net Framework2.0. (For more information about UniqueID, This article focuses on ID and ClientID)
2. code example explanation
Take a look at the following code:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default2.aspx. cs" Inherits = "Default2" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Untitled Page </title>
</Head>
<Script type = "text/javascript">
Function GetValue ()
{
<SPAN style = "COLOR: #0080c0"> <STRONG> var t = document. getElementById ('<% = TextBox1.ClientID %>'); </STRONG> </SPAN>
T. innerText = 2;
}
</Script>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox> </div>
<Input type = "button" runat = "server" id = "button1" onclick = "GetValue ();" value = "assign value"/>
</Form>
</Body>
</Html>

Someone may ask: var t = document. getElementById ("TextBox1"); isn't it running well?
Answer: In general aspx, ID = ClientID (provided that you have set the ID value yourself)
The following code sets the template page.
Copy codeThe Code is as follows:
<% @ Page Language = "C #" MasterPageFile = "~ /MasterPage. master "AutoEventWireup =" true "CodeFile =" Default3.aspx. cs "Inherits =" Default3 "Title =" Untitled Page "%>
<Asp: Content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" Runat = "Server">
<Script type = "text/javascript">
Function GetValue ()
{
<SPAN style = "COLOR: #0080c0"> <STRONG> document. write ('<% = TextBox1.ClientID %>') </STRONG> </SPAN>
}
</Script>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Input type = "button" runat = "server" id = "button1" onclick = "GetValue ();" value = "assign value"/>
</Asp: Content>
<SPAN style = "COLOR: #0080c0"> <STRONG> ctl00_ContentPlaceHolder1_TextBox1 </STRONG> </SPAN> is displayed. That is, TextBox1.ClientID = ctl00_ContentPlaceHolder1_TextBox1.

Change the code
Copy codeThe Code is as follows:
<% @ Page Language = "C #" MasterPageFile = "~ /MasterPage. master "AutoEventWireup =" true "CodeFile =" Default3.aspx. cs "Inherits =" Default3 "Title =" Untitled Page "%>
<Asp: Content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" Runat = "Server">
<Script type = "text/javascript">
Function GetValue ()
{
<SPAN style = "COLOR: #0080c0"> <STRONG> <SPAN style = "TEXT-DECORATION: line-through"> var t = document. getElementById ("TextBox1"); </SPAN> </STRONG> </SPAN>
T. innerText = 2;
}
</Script>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Input type = "button" runat = "server" id = "button1" onclick = "GetValue ();" value = "assign value"/>
</Asp: Content>
Error. t = null, that is, TextBox1 cannot be found. Therefore, you need to change it to <SPAN style = "COLOR: #0080c0"> <STRONG> var t = document. getElementById ('<% = TextBox1.ClientID %>'); </STRONG> </SPAN>

3. Summary
View sourceprint? 1. for server controls, the ClientID attribute is used when the client is called, and the ID attribute is used when the server is called.

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.