ASP. Call foreground JS Call backend code share

Source: Internet
Author: User
Tags call back

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.