1、C#调用JavaScript
(1)准备好JavaScript函数
<script type="text/javascript">
function say() {
alert("CSharp调用js,调用成功!");
}
</script>
(2)页面代码
<asp:Button ID="Button1" runat="server" Text="CSharp调用js" onclick="Button1_Click" />
(3)C#代码
protected void Button1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "say", "<script>say(); </script>");
}
2、JavaScript调用C#代码
(1)页面代码
<input type="button" value="js调用CSharp" onclick="jsCallCSharp();" />
<asp:Button ID="Button2" runat="server" Text="我是隐藏按钮" onclick="Button2_Click" style="display:none;"/>
(2)C#代码
//后台方法
public void Show()
{
Response.Write("js调用后台方法成功!");
}
//隐藏按钮事件
protected void Button2_Click(object sender, EventArgs e)
{
Show();
}
(3)JavaScript代码
<script type="text/javascript">
function jsCallCSharp() {
document.getElementById("Button2").click();
}
</script>
版权所有 2021-2024 石家庄建站平台 冀ICP备2022000585号-2 24小时服务热线:15383239821