Mutual access between javascript and cs code

Source: Internet
Author: User

It mainly includes four methods for intercommunication (including access to functions and variables) between javascipt and CS code in the background ).

 

1. Access C # functions using JavaScript

Execute functions in C # code in javaScript Functions:

Method 1:

A. For example, create a Button with ID "btn" and write the called or processed content to button_click in the background cs code;

B. Call document. getElementById ("btn"). click () in JavaScript on the HTML front-end page. This line of code calls the background button_click event.

Method 2:

A. Call the background Code directly on the HTML front-end page. The following background code is available:

   

Public string GetValue ()
{
Return "return value from. cs code ";
}

B. the following code calls the background method on the HTML page:

  

Function ShowValue ()
{
Var retVal = <% = GetValue () %>; // The background code is written in <%>.
Window. alert (retVal );
}

 

 

2. JavaScript accesses the C # variable

Method 1:

A. Hide the field access on the page. You can save the c # variable value in the background to the hidden text field.

<Input id = "xx" type = "hidden" runat = "server">

B. Then, directly obtain the value of the hidden text field in the front-end javascript.

Document. getElementById ('xx'). value

Method 2:

A. register the script on the page after assigning values to variables on the server.

Page. RegisterStartScript ("", "<script language = 'javascript '> var vary =" + value + "</script> ");

Value is the background variable, and then the vary value can be directly accessed in javascript. Its value is the value of the background variable value. This method only allows indirect access to the c # variable.

 

3. Access JavaScript variables in C #

Method 1:

A. Write the javascript variable value to the HTML page at the front end and use the static text control to hide the field;

B. the backend uses Request ["id"] to obtain the value;

Method 2:

Cookie or session can be used

 

4. Access JavaScript Functions in C #

Method 1:

A,Page. RegisterStartupScript ("function", "<script> The Name Of The javascript function to be called; </script> ");

Method 2:

B. Use LiteralControl to call the javascript function in Button_Click as follows.

Public void Button_Click (object sender, System. EventArgs e)
{
LiteralControl li = new LiteralControl ();
Li. Text = "<script> name of the javascript function to be called; </script> ";
Controls. Add (li );
}

 

I wrote it here. If you think it is useful to you, let's talk about it. In case of any errors, please note that you are not very grateful.

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.