JS Get server-side control ID

Source: Internet
Author: User

Many times we need to do some simple processing of the server-side control in JS, but this time there is no need to send back to the server, let the server to deal with, this time will have to use JS again

So how to get this server-side control? We know that the server eventually returns to the user interface is actually an HTML file, all the server controls finally become the normal meaning of HTML, must textbox will become a <input type= "text" ....> this time, The server side will also add an ID to the HTML tag, the previous write JS to get this ID is often used for this ID, but we can not always use this method to obtain? such as the FormView control, finally to the user here, it becomes a table tag, and then use the textbox inside the label template Ah, DropDownList ah, this is troublesome, ID will be this: **_formview1_****_******, But is this ID value OK? I don't know about that, but I do know that this is definitely not stable.

Then there are two methods:

1. Use the ClientID property of the Web control to bind the background control in the foreground JS, as follows:

function GetValue ()
{
var Txdat=document.getelementbyid (' <%=txtname.clientid%> ');

}

What's the use of the back? This gets the server-side ID that is correct and stable.

2, in the server-side register a JS script, in need to use this ID when to register, why? After the page has been sent back to the server, the server control's ID may change, resulting in unpredictable results, so if you need to use this control to register a piece of JS code to the page, and then call directly in the foreground is OK, for example:

RegisterStartupScript ("Check",
"\n<script>\n" +
"function check () \ n" +
"{\ n" +
"Return alert ('" + txtName.ClientID.ToString () + ""); \ n "+
"}\n" +
"</script>\n";

JS Get server-side 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.