I,JavascriptExecute C # In the Function #CodeFunctions in:
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 SS ()
{
Return ("");
}
2. You can call <% = fucntion () %> in HTML.
Foreground script
<Script language = JavaScript>
VaR A = "<% = SS () %> ";
Alert ();
</SCRIPT>
Method 3:
1. <script language = "JavaScript">
<! --
Function _ dopostback (eventtarget, eventargument)
{
VaR theform = Document. form1; // indicates the form of runat = server.
Theform. _ eventtarget. value = eventtarget;
Thefrom. _ eventargument. value = eventargument;
Theform. Submit ();
}
-->
</SCRIPT>
<Input id = "button1" type = "button" name = "button1" value = "button" onclick = "javascript :__ dopostback ('button1','') ">
Method 4:
<Script language = "JavaScript">
Function submitkeyclick ()
{
If (event. keycode = 13)
{
Event. cancelbubble = true;
Event. returnvalue = false;
Document. All. funname. value = "Name of the function you want to call ";
Document. Form [0]. Submit ();
}
}
</SCRIPT>
<Input onkeypress = "submitkeyclick ()" id = "AAA" type = "text">
<Input type = "hidden" name = "funname"> <〈! -- Used to store the function you want to call --> --〉
In. CS, there are:
Public page_onload ()
{
If (! Page. ispost ())
{
String strfunname = request. Form ["funname"]! = NULL? Request. Form ["funname"]: "";
// Determine which function to call based on the value returned
Switch (strfunname)
{
Case "Enter ()":
Enter (); // call this function
Break;
Case "Others ":
// Call other functions
Break;
Default:
// Call the default Function
Break;
}
}
}
Public void enter ()
{
//...... For example, calculating a value
}
2. How to access the C # variable in JavaScript?
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.
3. How to access existing JavaScript variables in C?
Method 1:
1. the foreground uses static text controls to hide fields and write JS variable values into them;
2. the backend uses request ["ID"] to obtain the value;
Method 2: cookie or session can be used
4. How to access JavaScript Functions in C?
C # execute JavaScript Functions in the Code:
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;
}
Appendix: shortcuts in ASP. NET)
Default. aspx file:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<SCRIPT type = "text/JavaScript">
Function btnclick ()
{
Alert ("called at the front end! ");
Document. getelementbyid ("button1"). Click ();
}
Function ctlent ()
{
If (event. altkey & event. keycode = 83 ))
{
Alert ("response button! ");
Btnclick ();
}
}
</SCRIPT>
<Body onkeydown = "ctlent ();">
<Form ID = "form1" runat = "server">
<Div>
<Asp: button id = "button1" runat = "server" text = "button" onclick = "button#click"/> </div>
</Form>
</Body>
</Html>
Default. aspx. CS file:
Protected void button#click (Object sender, eventargs E)
{
Response. Write ("<script language = 'javascript '> alert ('background response! '); </SCRIPT> ");
}