C # code and JavaScript functions call each other
Ask:
1. How do I access C # functions in JavaScript?
2. How do I access C # variables in JavaScript?
3. How do I access the existing variables of JavaScript in C #?
4. How do I access JavaScript functions in C #?
Question 1 answers are as follows:
The functions in the C # code are executed in the JavaScript function:
Method One: 1, first set up a button, in the background will be called or processed content written in Button_Click;
2, write a JS function in the foreground, the content is document.getElementById ("Btn1"). Click ();
3, in the foreground or background call JS function, fire click event, equal access to the background C # function;
Method Two: 1, function declaration is public
Background code (change public to protected also can)
public string SS () { return ("a"); }
2, in the HTML with <%=fucntion ()%> can call
Foreground script
<script language=javascript> var a = "<%=ss ()%>"; alert (a); </script>
Method Three: 1,
<script language= "JavaScript" > <!-- function __doPostBack (eventtarget, eventargument) { var theform = document. Form1; Refers to Runat=server form theform.__eventtarget.value = eventtarget; Thefrom.__eventargument.value = eventargument; Theform.submit (); } - </script> <input id= "Button1" type= "button" Name= "Button1" value= "button" onclick= "javascript : __doPostBack (' Button1 ', ') ' >
Method Four:
<script language= "JavaScript" >function Submitkeyclick () { if (Event.keycode = =) { Event.cancelbubble = true; Event.returnvalue = false; Document.all.funname.value= "The function name you want to invoke"; 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"]: "";//depending on the value returned, decide which function switch (strfunname) {case "Enter ()" is called (): Enter (); Call this function break;case "other"://Call other function break;default://call default function break;}}} public void Enter () {//... such as calculating a value}
Question 2: How do I access C # variables in JavaScript?
The answers are as follows:
Method One: 1, through the page hidden domain access <input id= "xx" type= "hidden" runat= "Server" >
Method Two: 1, such as the background defines the public STRING N; The format for referencing the variable in the foreground JS is ' <%=n%> ' or ' +<%=n%>+ '
Method Three: 1, or you can register a script on the page after the server-side variable is assigned a value
"<script language= ' JavaScript ' >var temp=" + tmp + "</script>"
TMP is a background variable, and then JS can directly access the temp to get the value.
3. How do I access the existing variables of JavaScript in C #?
The answers are as follows:
Method One: 1. The foreground uses the static text control to hide the field and writes the value of the JS variable to it;
2, backstage with request["id" to get the value;
Method Two: You can use a cookie or session
4. How do I access JavaScript functions in C #?
The answers are as follows:
Execute JavaScript functions in C # code:
Method One: 1,
Page.registerstartupscript ("GGG", "<script>setvisible (1);</script>");
Method Two: 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;