C # foreground JS call back code
Front desk JS
<script type= "Text/javascript" language= "JavaScript" >
function Ceshi ()
{
var a = "<%=getstr ()%>";
alert (a);
}
</script>
<input type= "button" onclick= "Ceshi ();" value= "JS Call back Code"/>
Background code
public string Getstr ()
{
string aa = "Hello, you!" ";
return AA;
}
C # backstage call foreground JS code
Front desk JS
<script type= "Text/javascript" language= "JavaScript" >
function Ceshi ()
{
var a = "Hello, guys!" "
alert (a);
}
</script>
<asp:button id= "Button1" runat= "server" text= "background call js" onclick= "Button1_Click"/>
Background code
protected void Button1_Click (object sender, EventArgs e)
{
If you have UpdatePanel, use the following code to call the foreground JS
Scriptmanager.registerstartupscript (UpdatePanel1, this. Page.gettype (), "", "Ceshi ();", true);
If not, the following code
This. Page.ClientScript.RegisterStartupScript (this. Page.gettype (), "", "<script>ceshi ();</script>", true);
}
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.getelementbyidx ("Btn1"). Click ();--master page: document.getElementById ("<%=txtName.ClientID%>");
3, invoking the JS function in the foreground or background, firing the Click event, equals accessing the background C # function;
Method Two:1, function declaration as public
Background code (change public to protected also can)
PublicStringSS ()
{
Return("A");
}
2, used in HTML.<%=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 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 name of the function you want to call";
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:
PublicPage_onload ()
{
If (! Page.ispost ())
{
string strfunname=request.form[ Funname "]!=null? Request.form[ "funname "]:
Reprint: http://www.cnblogs.com/diony/archive/2011/08/26/2154604.html
ASP. Call foreground JS Call backend code share