Js call background background call foreground method summary
- 2020-03-30 02:41:33
- OfStack
Js call background, background call foreground method summary
1. Execute functions in C# code in javaScript functions:
Method 1:1. First, create a button and write the contents of the call or process to Button1_Click in the background;
protected void Button1_Click(object sender, EventArgs e)
{
this.TextBox1.Text = "voodooer";
}
2. In the foreground, it can be called as follows:
<input type="button" value=" access C# The method of " onclick='document.getElementById("Button1").click();' />
Method 2:1. Declare the function as public or protected
public string ss()
{
return("voodooer");
}
2. Foreground call method
<script language=javascript>
var a = " <%=ss()%>";
alert(a);
</script>
Method 3:1, < Script language = "javascript" >
<!--
function __doPostBack(eventTarget, eventArgument)
{
var theForm = document.Form1; //Refers to the form of the runat = server
theForm.__EVENTTARGET.value = eventTarget;
theFrom.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
-->
</script>
<input type="button" value=" button " >
Method 4:
<script language="javascript">
function SubmitKeyClick()
{
if (event.keyCode == 13)
{
event.cancelBubble = true;
event.returnValue = false;
document.all.FunName.value=" The name of the function you want to call " ;
document.form[0].submit();
}
}
</script>
<INPUT type="text">
<input type="hidden" >
In.CS we have:
public Page_OnLoad()
{
if (!Page.IsPost())
{
string strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:"";
//Which function to call depends on the value returned
switch(strFunName)
{
case "enter()":
enter() ; //Call this function
break;
case " other ":
//Call other functions
break;
default:
//Calling the default function
break;
}
}
}
public void enter()
{
//... Let's say I calculate something
}
2. How do I access C# variables in JavaScript?
The answer is as follows:
Method 1:1, through the page to hide the domain access < Input type = "hidden" runat = "server" >
Method 2:1. For example, the background defines PUBLIC STRING N; The format of the reference to this variable in foreground js is' < % = n % > 'or "+ < % = n % > +"
Or you can register a script on the page after a server-side variable is assigned
"< Script language = 'javascript' > Var temp=" + TMP + "< / script>"
TMP is a background variable, and then js can access temp directly to get the value.
3. How do I access JavaScript's existing variables in C#?
The answer is as follows:
Method 1:1. The foreground USES the static text control to hide the field, and writes the js variable value into it;
2. The background USES request["id"] to get the value;
Method 2: you can use cookies or sessions
4. How do I access JavaScript functions in C#?
The answer is as follows:
Execute javaScript function in c# code:
Method one: 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;
}