ASP. NET frontend and backend call Method

Source: Internet
Author: User

1. Access C # functions using JavaScript
2. javascript accesses the C # variable
3. Access existing JavaScript variables in C #
4. How to access JavaScript Functions in C #
Access C # functions using JavaScript
Method 1,
1. Create a button to write the called or processed content to button_click in the background;
2. Write a JS function on the front-end with the content document. getelementbyid ("btn1"). Click ();
3. Call JS functions on the frontend or backend to stimulate the click event, which is equivalent to accessing the C # function in the background;
Method 2,
1. function declaration is public or protected CopyCode The Code is as follows: Public String Hello ()
{
Return ("Hello World ");
}

2. You can call <% = fucntion () %> in HTML.Copy codeThe Code is as follows: <HTML xmlns = "http://www.w3.org/1999/xhtml%22>
<Head runat = "server">
<Title> untitled page </title>
<Script language = "JavaScript" type = "text/JavaScript">
Function say ()
{
VaR strhello = "<% = Hello () %> ";
Alert (strhello );
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "say ();"/>
</Div>
</Form>
</Body>
</Html>

method 3, copy Code the code is as follows:

access the C # variable in Javascript
Method 1: 1. Access
method 2 through hidden fields on the page: 1. If Public String N is defined in the background; the format of the variable referenced in front-end JS is '<% = n %>' or "+ <% = n %> +"
method 3: 1. You can register a script
" after assigning values to variables on the server. "
TMP is the background variable, then, you can directly access temp in JS to obtain the value.
access to existing JavaScript variables in C #
Method 1: 1. the foreground uses static text controls to hide the fields and write the JS variable values into them;
2. the backend uses request ["ID"] to obtain the value.
Method 2: you can use cookies or session
C # To access JavaScript Functions
Method 1: 1. Page. registerstartupscript ("ggg", "");
Method 2: Use the literal class, and then copy Code the code is as follows: Private void button2_click (Object sender, system. eventargs e)
{< br> string STR;
STR = " ";
// literal1.visible = true;
literal1.text = STR;
}

Related Article

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.