The original Published time: 2008-10-13--from my Baidu article [imported by moving tools]
. How do I access C # functions in JavaScript?
2. How do I access C # variables in JavaScript?
3. How do I access the existing variables of JavaScript in C #?
4. How do I access JavaScript functions in C #?
Question 1 answers are as follows:
The functions in the C # code are executed in the JavaScript function:
Method One: 1, first set up a button, in the background will be called or processed content written in Button_Click;
2, write a JS function in the foreground, the content is document.getElementById ("Btn1"). Click ();
3, in the foreground or background call JS function, fire click event, equal access to the background C # function;
(If you want Button1 to not appear on the page, nested a div display is set to not display, you cannot use Visable=false, because getElementById may not find Button1 when using Visable=false)
<script type= "Text/javascript" >
function Bind ()
{
document.getElementById ("Button1"). Click ();
}
</script>
<a id= "Div1" href= "#" onclick= "Bind ()" > No refresh Change hidden button trigger </a>
<div style= "Display:none" >
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>
</div>
Method Two: 1, function declaration is public
Background code (change public to protected also can)
public string SS ()
{
return ("a");
}
2, in HTML with <%=fucntion ()% > can call
Foreground script
<script language=javascript >
var a = "<%=ss ()% > ";
Alert (a);
</script >
Method Three: 1, <script language= "JavaScript" >
<!--
Function __doPostBack ( Eventtarget, eventargument)
{
var theform = document. Form1; Refers to Runat=server's form
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 Four: <script language= "JavaScript" >
function Submitkeyclick ()
{
if (Event.keycode = = 13)
{
Event.cancelbubble = true;
Event.returnvalue = false;
Document.all.funname.value= "The name of the function you want to invoke";
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"]: "";
Determines which function to call based on the value returned
Switch (strfunname)
{
Case "Enter ()":
Enter (); Call this function
Break
Case "Other":
Calling other functions
Break
Default
Calling the default function
Break
}
}
}
public void Enter ()
{
...... such as calculating a value
}
Question 2: How do I access C # variables in JavaScript?
The answers are as follows:
Method One: 1, through the page hidden domain access <input id= "xx" type= "hidden" runat= "Server" >
Method Two: 1, such as the background defines the public STRING N; The format for referencing the variable in the foreground JS is ' <%=n% > ' or ' + <%=n% >+ '
Method Three: 1, or you can register a script on the page after the server-side variable is assigned a value
"<script language= ' JavaScript ' >var temp=" + tmp + "</script >"
TMP is a background variable, and then JS can directly access the temp to get the value.
3. How do I access the existing variables of JavaScript in C #?
The answers are as follows:
Method One: Recommend the use of controls
Add code to JS
document.getElementById ("HiddenField1"). Value = ' wind herd ';
In the background code, you can use Hiddenfield1.value directly to get
Use
In the background code, you can use Leslie directly. Value obtained
Method Two: You can use a cookie or session
4. How do I access JavaScript functions in C #?
The answers are as follows:
Execute JavaScript functions in C # code:
Method One: 1, page.registerstartupscript ("GGG", "<script >setvisible (1); </script >");
Method Two: Use the literal class, and then
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;
}
JS and CS Visits "background foreground code calls JavaScript variables and JavaScript call code variables"