C#前台js调用后台代码和后台代码调用前台js
C#前台js调用后台代码
前台js
<script type="text/javascript" language="javascript"> function Ceshi() { var a = "<%=Getstr()%>"; alert(a); }</script><input type="button" onclick="Ceshi();" value="js调用后台代码" />
后台代码
public string Getstr(){ string aa = "你们好啊!"; return aa;}
C#后台调用前台js代码
前台js
<script type="text/javascript" language="javascript"> function Ceshi() { var a = "你们好啊!" alert(a); }</script><asp:Button ID="Button1" runat="server" Text="后台调用js" onclick="Button1_Click" />
后台代码
protected void Button1_Click(object sender, EventArgs e) { //如果有UpdatePanel就用如下代码调用前台js ScriptManager.RegisterStartupScript(UpdatePanel1, this.Page.GetType(), "", "Ceshi();", true); //如果没有就如下代码 this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<script>Ceshi();</script>", true); }