This article summarizes the interactive methods of. NET and JavaScript scripts and shares it for everyone's reference. Here's how:
1.asp.net Call JS
Response.Write ("<script language=javascript>"); Response.Write ("alert (' Welcome to Dengfeng ');"); Response.Write ("location.href= ' login.aspx ';"); Response.Write ("</script>");
In this case, you can call the function of the JS script in the page to
2. How the JS script accesses the value of the server control
The interface has a TextBox control, ID Name,js can use the following script to take the value of name
var myvalue=document.all (' Name '). Value;
3, server control how to take the value of the variable in JS
The method is to put a hidden control HtmlInputHidden on the interface and then set to run as a server control so that the value of the control can be accessed in the JS script and in the ASP.
To assign a value to a server control in JS:
var bt=document.all (' name '). value;bt.value= ' name ';
Asp. NET, accessed using Name.value.
4. Calls between foreground and background functions
CS File:
C # code
The page class inherits the ICallbackEventHandler interface and implements two of these methods public partial class _default:system.web.ui.page, icallbackeventhandler{ private string m_strresult = ""; #region ICallbackEventHandler Memberspublic string GetCallbackResult () {// Return server-side processing results to Receiveserverresult method return m_strresult;} public void RaiseCallbackEvent (string eventargument) {///eventargument is a variable from the client that corresponds to the ARG variable//Add server-side processing logic here ... m_ strresult = eventargument;} #endregion}
The functions in the C # code are executed in the 5.javaScript function:
Method One:
①, first set up a button, in the background will be called or processed content written in Button_Click;
②, write a JS function in the foreground, the content is
document.getElementById ("Btn1"). Click ();
③, invoking the JS function in the foreground or background, firing the Click event, equals accessing the background C # function;
Method Two:
①, function declaration as public
Background code (change public to protected also can)
public string SS () {return ("a");}
②, in HTML with <%=fucntion ()%> can be called
Foreground script
<script Language=javascript>var A = "<%=ss ()%>"; alert (a);</script>
Method Three:
①
<script language= "javascript" ><!--function __doPostBack (eventtarget, eventargument) {var theform = document. Form1; Refers to runat=server Formtheform.__eventtarget.value = Eventtarget;thefrom.__eventargument.value = EVENTARGUMENT; Theform.submit ();} --></script><input id= "Button1" type= "button" Name= "Button1" value= "pushbutton" onclick= "Javascript:dopostback ( ' Button1 ', ') ' >
Method Four:
<script language= "JavaScript" >function Submitkeyclick () {if (Event.keycode = =) {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 "> <!--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"]: "";//depending on the value returned, decide which function switch (strfunname) {case "Enter ()" is called (): Enter (); Call this function break;case "other"://Call other function break;default://call default function break;}}} public void Enter () {//... such as calculating a value}
6. Accessing C # Variables in JavaScript
Method One: Access by hiding the domain on the page
<input id= "xx" type= "hidden" runat= "Server" >
Method Two: As the background defines the public STRING N; The format for referencing this variable in the foreground JS is ' <%=n%> ' or ' +<%=n%>+ '
Method Three: 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.
7. Accessing JavaScript functions in C #
Execute JavaScript functions in C # code:
Method One:
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;}
I hope this article is helpful to everyone's C # programming.