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