HowJavaScriptAccess the backgroundC #Methods and variables
Method 1: Use the Ajax development framework and add [AjaxPro. AjaxMethod] before defining the background method. Then, you can call the background C # function in the foreground js script.
Method 2: the background method is declared as public or protected, and then the foreground uses the js script for calling.
. Cs
Public string Str ()
{
Return "execute the C # method in the background in the javaScript function ..";
}
. Aspx
<Script type = "text/javascript">
Var a = "<% = Str () %> ";
Alert ();
</Script>
Method 3: Use Session Variables
. Cs
If (Session ["siteName"] = null) // determines whether a Session variable with the specified Key value exists.
Session ["siteName"] = ""; // creates a Session variable if it does not exist.
// Assign a value to the Session ["siteName"] variable
. Aspx
Var siteName = "<% = Session [" siteName "] %> ";
Method 4
<Asp: Button ID = "Button1" runat = "server" onclick = "button#click" style = "display: none;"/>
<Input type = "button" id = "btnSave" value = "save" onclick = "GetBehind (); return false;"/>
. Aspx. cs
<Script type = "text/javascript">
// Call the background Method
// Use this method to call the background code. You can perform some verification on the foreground before calling the code.
Function GetBehind (){
Document. getElementById ("Button1"). click ();
}
</Script>
. Cs
Protected void button#click (object sender, EventArgs e)
{}
How to log on to the backendC #Code ExecutionJavaScriptFunction
Method 1: void Page. RegisterStartupScript (string key, string script );
Method 2: Use the Hidden domain or Literal control to write the values controlled by some js functions into the Hidden domain or Literal control in the foreground, and then use Hidden in the foreground. value or Literal. text reads the foreground value.
. Aspx
Function GetTitleID (obj)
{
STitleID = obj
If (sTitleID! = Null)
Document. getElementById ("HiddenField1"). value = type + ',' + sTitleID;
Else
Document. getElementById ("HiddenField1"). value = type + ', 0 ';
}
. Cs
String hiddenValue = this. HiddenField1.Value;