1. How to access the C # function 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?
We will answer your questions through Q &.C # interaction with JavaScript Functions: 1. How to access the C # function 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?
The answer to question 1 is as follows:
JavascriptExecute C # In the Function #CodeFunctions in:
Method 1: 1. Create a button to write the called or processed content to button_click in the background;
2. Write a JS function on the front-end with the content document. getelementbyid ("btn1"). Click ();
3. Call JS functions on the frontend or backend to stimulate the click event, which is equivalent to accessing the C # function in the background;
Method 2: 1. function declaration is public
Background code (change public to protected)
The following is a reference clip: Public String SS () { Return (""); } |
2. can be called in HTML
Foreground script
The following is a reference clip: <Script language = JavaScript> VaR A = "<% = SS () %> "; Alert (); </SCRIPT> |
Method 3: 1,
The following is a reference clip: Theform. Submit (); } --> </SCRIPT> <Input id = "button1" type = "button" name = "button1" value = "button" onclick = "javascript :__ dopostback ('button1','') "> |
Method 4:
The following is a reference clip: 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:
The following is a reference clip: Public page_onload () { If (! Page. ispost ()) { String strfunname = request. Form ["funname"]! = NULL? Request. Form ["funname"]: ""; // Determine which function to call based on the value returned Switch (strfunname) { Case "Enter ()": Enter (); // call this function Break; Case "Others ": // 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: 1. If the backend defines public string N, the format of the variable referenced in front-end JS is ''or" ++"
Method 3: 1. You can register a script on the page after assigning values to variables on the server.
"<Script language = 'javascript '> var temp =" + TMP + "</SCRIPT>" tmp is a background variable, and you can directly access temp in JS to obtain the value.
Question 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 field and write the JS variable value into it;
2. the backend uses request ["ID"] to obtain the value;
Method 2: cookie or session can be used
Question 4. How to access JavaScript Functions in C?
The answer is as follows:
C # execute JavaScript Functions in the Code:
Method 1: 1. Page. registerstartupscript ("ggg ","");
Method 2: Use the literal class and
The following is a reference clip: private void button2_click (Object sender, system. eventargs e) {< br> string STR; STR = ""; // literal1.visible = true; literal1.text = STR; } |