C # interaction with JavaScript
In Asp.net development, the background and foreground interactions are often used. In this case, a summary of how C # and JavaScript interact with each other is provided.
1. In the background C #CodeTo call jacascript.
JavaScript code:
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function Test ()
{
Alert ("oec2003 ");
Return false;
}
</SCRIPT>
C # code:
Protected void button#click (Object sender, eventargs E)
{
Clientscript. registerstartupscript (this. GetType (), "clear", "<SCRIPT> test () </SCRIPT> ");
}
2. Call the C # method in Javascript
If the method in C # has a return value, you can use the following method:
C # code
Public String getauthstatus ()
{
Viewstate ["auth"] = "red ";
Return viewstate ["auth"]. tostring ();
}
JavaScript code
Function getauth ()
{
VaR authstatus = "<% = getauthstatus () %> ";
Return authstatus;
}
If the C # method called by JavaScript does not return a value, you can put a button on one side and then write what you want to do in the Click Event of the button, write the following code in the client script.
Document. All ("button1"). Click ();