Obtain the control clientid (get control reference from external JavaScript) of the ASPX page from an external JS file. When using a container such as masterpage and usercontrol, asp.net automatically generates a clientid for the control in the container (which can be generated in the control tree, otherwise it will not be generated ). For example, in contentplaceholder1, button1 generates the clientid "ctl00_contentplaceholder1_button1" by default. What we see in the mark up output by render is also the clientid. Therefore, when we use JavaScript to operate on the control elements, we must use clientid to search for the control. Inline solution if Javascript CodeIn the. aspx file, that is, in the inline script. When the page is generated, We can bind the clientid of the control to the page mark up through the binding mechanism, so we can use: document. getelementbyid ("<% = me.txt test. clientid %> ") to obtain the real reference of a control. Of course, methods such as findcontrol can also be written in <% =... to BIND server data to the client. External JS solutions. However, in some cases, to decouple JavaScript, we often write Javascript in the. js file separately and reference it to the aspx file. In this case, the code in the. js file cannot be bound to the server data through <% =... %>, so the above method cannot be used. In this case, the simple solution is to directly write the clientid of the control in Javascript, but this increases the coupling between JS files and aspx, which is not recommended. There are two methods that I commonly use. Here, I will introduce them: Case: default5.aspx is the content page of masterpage. Master, the main file in this example. JScript. JS is an external JS file used to process JavaScript operations. Button1 is an <asp: button> In default5.aspx to display the effect. Button2 is a <input type = button> In default5.aspx to trigger JavaScript. Requirement: Click button2 and change the text on button1 to "from extended JS". solution 1: Use the inline JS accessors to obtain the clientid dynamically generated by aspx in external Js, you can add accessors on the aspx page, similar to the properties in the OO language. We Add the following code in default5.aspx: Purpose: ① declare the getclientid accesser, and register the clientid of button1. ② Reference JScript. JS file <SCRIPT type = "text/JavaScript"> function getclientid () {var paraid1 = '<% = button1.clientid %>'; return {id1: paraid1 };} </SCRIPT> <SCRIPT type = "text/JavaScript" src = "jscript. JS "> </SCRIPT> next, we will go to JScript. in JS, you can achieve the requirement as follows: function changetext () {var BTN = document. getelementbyid (getclientid (). id1); BTN. value = "from extended JS";} getclientid (). id1 looks very Oo, and it also supports the JS smart prompt that vs2008 is very poor. then you can select the ID in the prompt 1. if multiple controls need to be registered, just register them in the accessors. Below is a complete DEMO code: default5.aspx <% @ page Language = "C #" masterpagefile = "~ /Masterpage. master "Title =" untitled page "%> <SCRIPT runat =" server "> </SCRIPT> <asp: content ID = "content1" contentplaceholderid = "head" runat = "server"> <SCRIPT type = "text/JavaScript"> function getclientid () {var paraid1 = '<% = button1.clientid %>'; // Register Control 1 var paraid2 = '<% = textbox1.clientid %>'; // Register Control 2 Return {id1: paraid1, Id2: paraid2}; // generate accessors} </SCRIPT> <SCRIPT type = "text/JavaScript" src = "jscript. JS "> </ SCRIPT> // reference external JS </ASP: content> <asp: Content ID = "content2" contentplaceholderid = "contentplaceholder1" runat = "server"> <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <asp: button id = "button1" runat = "server" text = "button"/> <input id = "button2" type = "button" value = "button" onclick = "changetext (); "/> </ASP: content> JScript. jsfunction changetext () {var BTN = document. getelementbyid (getclientid (). id1 ); BTN. value = "from extended JS"; var BTN = document. getelementbyid (getclientid (). id2); BTN. value = "from extended JS";} solution 2: Using JS global variables also compares Oo, that is, using JS global variables. Similarly, you also need to add a JS Code in defa5.5.aspx as a global variable to provide clientid: <SCRIPT type = "text/JavaScript"> var globals = {}; globals. controlidentities ={}; globals. controlidentities. somecontrol1 = '<% = button1.clientid %>'; globals. controlidentities. somecontrol2 = '<% = textb Ox1.clientid %> '; </SCRIPT> <SCRIPT type = "text/JavaScript" src = "jscript. JS "> </SCRIPT> next, we will go to JScript. in JS, you can achieve the requirement as follows: function changetext () {var BTN = document. getelementbyid (globals. controlidentities. somecontrol1); BTN. value = "from extended JS";} globals. controlidentities. somecontrol1, similarly, also supports the JS smart prompt that vs2008 is very poor, with ". "Then you can select somecontrol1 in the prompt. The following is a complete DEMO code: default5.aspx <% @ page Language =" C # "masterpagefile = "~ /Masterpage. master "Title =" untitled page "%> <SCRIPT runat =" server "> </SCRIPT> <asp: content ID = "content1" contentplaceholderid = "head" runat = "server"> <SCRIPT type = "text/JavaScript"> var globals ={}; globals. controlidentities ={}; globals. controlidentities. somecontrol1 = '<% = button1.clientid %>'; globals. controlidentities. somecontrol2 = '<% = textbox1.clientid %>'; </SCRIPT> <SCRIPT type = "text/JavaScript" src = "jscript. JS "> </SCRIPT> </ASP: content> <asp: Content ID =" content2 "contentplaceholderid =" contentplaceholder1 "runat =" server "> <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <asp: button id = "button1" runat = "server" text = "button"/> <input id = "button2" type = "button" value = "button" onclick = "changetext (); "/> </ASP: content> JScript. jsfunction changetext () {var BTN = document. getelementbyid (globals. controlidentities. somecontrol1); BTN. value = "from extended JS"; var TXT = document. getelementbyid (globals. controlidentities. somecontrol2); BTN. value = "from extended JS ";}
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