ASP. NET JS get control ID

Source: Internet
Author: User

Take the text box as an example: First, no master page is used

JS can be in the form of document.getElementById (' control's ID ') or document.getElementById (' <%= control's Id.clientid%> '), the code is as follows:

1"Server">2<title></title>3<script type="Text/javascript">4 function Change () {5             varControl = document.getElementById ('TextBox1');6             //or the following way.7             //var control = document.getElementById (' <%=textbox1.clientid%> ');8             if(Control.value! ="") {9Alert"the text entered:"+control.value);Ten             } One         } A</script> - -<body> the<form id="Form1"runat="Server"> -<div> -<asp:textbox id="TextBox1"runat="Server"Onblur="Change ()"></asp:TextBox> -</div> +</form> -</body>

In the case of HTML <input> tags, the way the ID is obtained and the server control obtains the ID, the code is as follows:

1"Server">2<title></title>3<script type="Text/javascript">4 function Change () {5             varControl = document.getElementById ('Text1');6             if(Control.value! ="") {7Alert"the text entered:"+control.value);8             }9         }Ten</script> One A<body> -<form id="Form1"runat="Server"> -<div> the<input id="Text1"Type="text"Onblur="Change ()"/> -</div> -</form> -</body>

Note: Use the HTML <input> tag to get its value in the background (string text = this.) Text1.value;), or in JS with document.getElementById (' <%=text1.clientid%> '), the form of getting the control ID, need to become a server control, plus runat= "Server", That is <input id= "Text1" type= "text" runat= "server" onblur= "Change ()" >, of course, even with runat= "server", JS can also use document.getElementById (' Text1 '); get its ID.

Second, use master page

① for standard server controls, such as <ASP:TEXTBOX>,JS, you must use document.getElementById (' <%=textbox1.clientid%> ') to get its control ID, and get its ID. The code is as follows:

1<asp:content id="Content1"Contentplaceholderid="Head"runat="Server">2<script type="Text/javascript">3 function Change () {4         //var control = document.getElementById (' TextBox1 ');//the TextBox1 object was not found5         varControl = document.getElementById ('Textbox1.clientid');6         if(Control.value! ="") {7Alert"the text is:"+control.value);8         }9     }Ten</script> One</asp:Content> A<asp:content id="Content2"Contentplaceholderid="ContentPlaceHolder1"runat="Server"> -<asp:textbox id="TextBox1"runat="Server"Onblur="Change ()"></asp:TextBox> -</asp:Content>

If you use document.getElementById (' TextBox1 '), the following error occurs:

② for HTML <input> tags, if you have runat= "server" as a server control, JS gets its ID using document.getElementById (' <%=text1.clientid%> ' ); If not as a server control, use document.getElementById (' Text1 '); get its ID. Otherwise the error will also occur, the code is as follows:

1<asp:content id="Content1"Contentplaceholderid="Head"runat="Server">2<script type="Text/javascript">3 function Change () {4         varControl = document.getElementById ('Text1.clientid');//get its ID as a server control5         //var control = document.getElementById (' Tex2 ');//as an HTML <input> tag, get its ID6         if(Control.value! ="") {7Alert"the text is:"+control.value);8         }9     }Ten</script> One</asp:Content> A<asp:content id="Content2"Contentplaceholderid="ContentPlaceHolder1"runat="Server"> -<input id="Text1"Type="text"runat="Server"Onblur="Change ()"/> -<%--<input id="Text2"Type="text"Onblur="Change ()"/>--%> the</asp:Content>

Note: If you want to get its text value in the background, you must use it as a server control, plus runat= "Server"

③ for server Controls , if you add clientidmode= "Static" property to the ContentPlaceHolder control inside the master page body , The content page JS gets the server control ID can be used directly with its ID name (without using the <%= control Id.clientid%>), the master page code is as follows:

1"Server">2<title></title>3<asp:contentplaceholder id="Head"runat="Server">4</asp:ContentPlaceHolder>56<body>7<form id="Form1"runat="Server">8<div>9<asp:contentplaceholder id="ContentPlaceHolder1"runat="Server"Clientidmode="Static">Ten          One</asp:ContentPlaceHolder> A</div> -</form> -</body>

The content page code is as follows:

1<asp:content id="Content1"Contentplaceholderid="Head"runat="Server">2<script type="Text/javascript">3 function Change (ID) {4         varControl =document.getElementById (ID);5         if(Control.value! ="") {6Alert"the text is:"+control.value);7         }8     }9</script>Ten</asp:Content> One<asp:content id="Content2"Contentplaceholderid="ContentPlaceHolder1"runat="Server"> A<input id="Text1"Type="text"runat="Server"Onblur="Change (this.id)"/><br/> -<asp:textbox id="TextBox1"runat="Server"Onblur="Change (this.id)"></asp:TextBox> -</asp:Content>

ASP. NET JS get control ID

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.