Asp.net foreground javascript and background C # Interaction

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.