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 #
JavascriptAccess C # Functions
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
Public String Hello ()
{
Return ("Hello World ");
}
2. You can call <% = fucntion () %> in HTML.
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<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,
<Script language = "JavaScript">
Function _ dopostback (eventtarget, eventargument)
{
VaR frontform = Document. form1; // indicates the form of runat = server.
Frontform. _ eventtarget. value = eventtarget;
Frontform. _ eventargument. value = eventargument;
Frontform. Submit ();
}
</SCRIPT>
<Input id = "button1" type = "button" name = "button1" value = "button" onclick = "javascript :__ dopostback ('button1','') ">
JavascriptAccess the C # variable
Method 1: 1. Access <input id = "XX" type = "hidden" runat = "server">
Method 2: 1. For example, 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 on the page after assigning values to variables on the server.
"<Script language = 'javascript '> var temp =" + TMP + "</SCRIPT>"
TMP is the background variable, and then you can directly access temp in JS to obtain the value.
C #Accessing existing JavaScript Variables
Method 1: 1. the foreground uses static text controls to hide the field and write the JS variable value into it;
2. the backend uses request ["ID"] to obtain the value;
Method 2: cookie or session can be used
C #Accessing JavaScript Functions
Method 1: 1. Page. registerstartupscript ("ggg", "<SCRIPT> setvisible (1); </SCRIPT> ");
Method 2: Use the literal class and
Private void button2_click (Object sender, system. eventargs E)
{
String STR;
STR = "<script language = 'javascript '> ";
STR + = "selectrange ()";
STR + = "</SCRIPT> ";
// Literal1.visible = true;
Literal1.text = STR;
}