asp.net the Id,clientid,uniqueid of the server control _ practical Tips

Source: Internet
Author: User
1. Brief introduction
ID is the ID you specify when designing, is the programming identifier we assign to the server control, we often use This.controlid to find the control, then this controlid is the ID here.
ClientID is the client-side identifier of the server control generated by ASP.net, and when the control is generated to the client page, it needs to be accessed by ClientID when the client code accesses the control.
UniqueID a unique, hierarchical, qualified identifier for a server control. It is used when you need to participate in the service side return. When you place a control in a repeating control (Repeater, DataList, and DataGrid), multiple server-side controls may be generated, which requires distinguishing the individual controls on the server side so that their ID properties do not conflict. UniqueID is generated by connecting the UniqueID value of the child control's parent control to the control's ID value, and the characters are concatenated between the parts, as specified by the Idseparator property. By default, the Idseparator property is a colon character (:). This property is a new addition to the. Net Framework2.0 species. (UniqueID's explanations are referenced, this article focuses on IDs and ClientID)
2, code example explanation
Look at the following code:
Copy Code code 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 ">
<title>untitled page</title>
<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=" Assignment/>
</form>
</body>

Some people will ask: Var T=document.getelementbyid ("TextBox1"); not also run well?
Answer: In general aspx Id=clientid (if you have set the ID value yourself)
Look at the code below, set the template page
Copy Code code 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=" Assignment/>
</asp:Content>
The page shows the <span style= "COLOR: #0080c0" ><strong>ctl00_contentplaceholder1_textbox1</strong></span >. namely Textbox1.clientid =ctl00_contentplaceholder1_textbox1.

Now change the code to
Copy Code code 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=" Assignment/>
</asp:Content>
Error, T=null, that is, can not find TextBox1, so need to change to <span style= "COLOR: #0080c0" ><strong>var T=document.getelementbyid (' <%=TextBox1.ClientID%> ');</strong></span>

3. Summary
View sourceprint?1 for server controls, use the ClientID property when the client is tuned, and use the id attribute on the service side.

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.