Asp. NET in foreground JavaScript and background code calls
1 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 #?
The answer to question 1 is as follows:
To execute functions in C # code in JavaScript functions:
Method One: 1, first establishes a button, in the backstage will call or the processing content writes in the 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
The following are the referenced contents:
Background code (change public to protected also can)
public string SS ()
{
Return ("a");
}
2, in the HTML used to 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 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 Four:
<script language= "JavaScript" >
function Submitkeyclick ()
{
if (Event.keycode = 13)
{
Event.cancelbubble = true;
Event.returnvalue = false;
Document.all.funname.value= "The function name you want to call";
Document.form[0].submit ();
}
}
</script>
<input onkeypress= "Submitkeyclick ()" id= "AAA" type= "text" >
<input type= "hidden" name= "Funname" >〈! --To store the function you want to call--〉
In the. cs there are:
The following are the referenced contents:
Public Page_onload ()
{
if (! Page.ispost ())
{
String strfunname=request.form["Funname"]!=null? request.form["Funname"]: "";
Decide which function to call based on the returned value
Switch (strfunname)
{
Case "Enter ()":
Enter (); Call this function
Break
Case "Other":
Calling other functions
Break
Default
Calling the default function
Break
}
}
}
public void Enter ()
{
...... Like calculating a value
}
Question 2. How do I access C # variables in JavaScript?
The answer is 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 defined public STRING N; The format for referencing the variable in the foreground JS is ' or ' + + '
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.
Question 3. How do I access the existing variables of JavaScript in C #?
The answer is as follows:
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
Question 4. How do I access JavaScript functions in C #?
The answer is as follows:
Execute JavaScript functions in C # code:
Method One: 1,
Page.registerstartupscript ("GGG", "");
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= "";
Literal1.visible=true;
LITERAL1.TEXT=STR;
}