Home > Article > Web Front-end > Summary of how javascript calls front and back method instances
JS calls the background, the background calls the frontend, etc. Summary
1. Execute the function in the C# code in the javaScript function:
Method 1: 1. First establish A button writes the calling or processing content into Button1_Click in the background;
protected void Button1_Click(object sender, EventArgs e) { this.TextBox1.Text = "voodooer"; }
2. It can be called like this in the foreground:
<input type="button" value="访问C#的方法" onclick='document.getElementById("Button1").click();' />
Method 2: 1. The function is declared as public or protected
public string ss() { return("voodooer"); }
2. Foreground calling method
<script language=javascript> var a = " <%=ss()%>"; alert(a); </script>
Method three: 1. 1a24b17f85e15c83fc29b7760e40bfcb
<!-- function __doPostBack(eventTarget, eventArgument) { var theForm = document.Form1; //指runat=server的form theForm.__EVENTTARGET.value = eventTarget; theFrom.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } --> </script> <input type="button" value="按钮" >
Method four:
<script language="javascript"> function SubmitKeyClick() { if (event.keyCode == 13) { event.cancelBubble = true; event.returnValue = false; document.all.FunName.value="你要调用的函数名"; document.form[0].submit(); } } </script> <INPUT type="text"> <input type="hidden" > 〈!--用来存储你要调用的函数 --〉
In .CS there are:
public Page_OnLoad() { if (!Page.IsPost()) { string strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:""; //根据传回来的值决定调用哪个函数 switch(strFunName) { case "enter()": enter() ; //调用该函数 break; case "其他": //调用其他函数 break; default: //调用默认函数 break; } } } public void enter() { //……比如计算某值 }
2. How to access C# variables in JavaScript?
The answer is as follows:
Method 1: 1. Access through hidden fields on the pageab45338d07709151a05c0c755f1cb555
Method 2: 1. If PUBLIC STRING N is defined in the background; the format for referencing this variable in the front-end js is' ed6d00bd7543ae3a0702774e79a7199d 'or "+ ed6d00bd7543ae3a0702774e79a7199d+"
Method 3: 1. Or you can register a script on the page after assigning values to server-side variables
" <script language='javascript'>var temp=" + tmp + " </script>"
tmp It is a background variable, and then you can directly access temp in js to get the value.
3. How to access existing variables of JavaScript in C#?
The answer is as follows:
Method 1: 1. Use static text control to hide the field in the front desk and change the js variable Write the value into it;
2. Use request["id"] in the background to get the value;
Method 2: You can use cookie or session
4. How to use C# Access JavaScript functions in?
The answer is as follows:
Execute javaScript functions in c# code:
Method 1: 1. Page.RegisterStartupScript("ggg"," 4b8b7004255070b474f8369177dd9b31");
Method 2: Use Literal class, 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; }
The above is the detailed content of Summary of how javascript calls front and back method instances. For more information, please follow other related articles on the PHP Chinese website!