1.JavaScript accessing C # functions
2.JavaScript accessing C # variables
An existing variable that accesses JavaScript in 3.c#
4. How to access JavaScript functions in C #
JavaScript Access C # functions
method One,
1. First establish a button, in the background will call or processing content written to Button_Click;
2, in the foreground to write a JS function, content for document.getElementById ("BTN1"). Click ();
3, in the foreground or backstage call JS function, fire click event, equal access to the background C # function;
method Two,
1. function declared as public or protected
Copy Code code as follows:
public string Hello ()
{
Return ("Hello World");
}
2, in HTML with <%=fucntion ()%> can call
Copy Code code as follows:
<title>untitled page</title>
<script language= "javascript" type= "Text/javascript" >
function Say ()
{
var Strhello = "<%=hello ()%>";
alert (Strhello);
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "Say ();"/>
</div>
</form>
</body>
methods Three,
Copy Code code as follows:
<script language= "JavaScript" >
function __doPostBack (Eventtarget, eventargument)
{
var frontform = document. Form1; Refers to 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 ', ')" >
Accessing C # Variables in JavaScript
Method One: 1, through the page hidden domain access <input id= "xx" type= "hidden" runat= "Server" >
Method Two: 1, such as the background definition of public STRING N; foreground JS reference the variable in the format of ' <%=n%> ' or ' +<%=n%>+ '
Method Three: 1, or you can register a script on the page after the server-side variable is assigned
"<script language= ' JavaScript ' >var temp=" + tmp + "</script>"
TMP is a background variable, and then you can directly access the temp to get the value in JS.
Existing variables that access JavaScript in C #
Method One: 1, the foreground uses the static text control to hide the domain, writes the JS variable value to it;
2, the backstage uses request["the ID" to obtain the value;
Method Two: Can use Cookie or session
accessing JavaScript functions in C #
Method One: 1, page.registerstartupscript ("GGG", "<script>setvisible (1);</script>");
Method Two: Use the literal class, and then
Copy Code code as follows:
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;
}