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);
}