In asp.net, how does one pass the variable value in the foreground script to the background code?

Source: Internet
Author: User
C # mutual calls between code and javaScript Functions: 1. How to access C # functions in JavaScript? 2. How to access the C # variable in JavaScript? 3. How to access existing JavaScript variables in C? 4. How to access JavaScript Functions in C? Question 1 answer: execute functions in C # code in javaScript Functions: Method 1: 1. Create a button to write the called or processed content to button_click in the background; 2. Write a js function at the front end with the content of document. getElementById ("btn1 "). click (); 3. Call the js function on the front or back end to stimulate the click event, which is equivalent to accessing the c # function in the background; Method 2: 1. function declaration as public background code (change public to protected) public string ss () {return ("");} 2. You can use <% = fucntion () %> in html to call the foreground script <script language = javascript> var a = "<% = ss () %> "; alert (a); </script> method 3: 1. <script language = "javascript"> <! -- Function _ doPostBack (eventTarget, eventArgument) {var theForm = document. form1; // indicates the form theForm of runat = server. _ EVENTTARGET. value = eventTarget; theFrom. _ EVENTARGUMENT. value = eventArgument; theForm. submit () ;}--> </script> <input id = "Button1" type = "button" name = "Button1" value = "button" onclick = "javascript: __dopostback ('button1', '')"> asp. N ET automatically generates the following script for the page: <input type = "hidden" name = "_ EVENTTARGET" id = "_ EVENTTARGET" value = ""/> <input type = "hidden" name = "_ EVENTARGUMENT "id =" _ EVENTARGUMENT "value =" "/> the two parameters are saved by the hidden form fields _ EVENTTARGET and _ EVENTARGUMENT. The two hidden forms can be used to find the control ID and parameters that cause the page delivery: string target = Request. params ["_ EVENTTARGET"]; string args = Request. params ["_ EVENTARGUMENT"]; asp: Button and asp: ImageButton do not use _ doPostBack to send back pages. Therefore, the above Code is invalid when you use these two controls to send back pages. Method 4: <script language = "javascript"> function SubmitKeyClick () {if (event. keyCode = 13) {event. cancelBubble = true; event. returnValue = false; document. all. funName. value = "Name of the function you want to call"; document. form [0]. submit () ;}</script> <INPUT onkeypress = "SubmitKeyClick () "id =" aaa "type =" text "> <input type =" hidden "name =" FunName "> <〈! -- Used to store the function you want to call --> in. CS, there are: public Page_OnLoad () {if (! Page. IsPost () {string strFunName = Request. Form ["FunName"]! = Null? Request. form ["FunName"]: ""; // you can call the switch (strFunName) {case "enter ()": enter () function based on the value returned (); // call this function break; case "other": // call other functions break; default: // call the default function break ;}} public void enter () {//...... For example, calculating a value} Question 2. How to access the C # variable in JavaScript? The answer is as follows: Method 1: 1. Access <input id = "xx" type = "hidden" runat = "server"> method 2 through a hidden domain on the page: 1. If public string n is defined in the background; the format of the variable referenced in front-end js is '<% = n %>' or "+ <% = n %> +" method 3: 1. You can register a script "<script language = 'javascript '> var temp =" + tmp + "</script>" tmp is background variable, then, you can directly access temp in js to obtain the value. 3. How to access existing JavaScript variables in C? The answer is as follows: Method 1: 1. the foreground uses static text controls to hide the domain and write the js variable value into it. 2. The background uses request ["id"] to obtain the value. Method 2: cookie or session4. how can I access JavaScript Functions in C? The answer is as follows: c # execute javaScript Functions in the Code: Method 1: 1. Page. registerStartupScript ("ggg", "<script> SetVisible (1); </script>"); Method 2: Use the Literal class, and then private void Button2_Click (object sender, System. eventArgs e) {string str; str = "<script language = 'javascript '>"; str + = "selectRange ()"; str + = "</script> "; // Literal1.Visible = true; Literal1.Text = str ;}

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.