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;
}