C#后端调用前端的方法
2024-08-31 21:34:52
在我实际开发过程中,刚好遇到c#后端要调用前端js中的方法,所以研究了一下,特分享如下:
前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="netToWebFantion.aspx.cs" Inherits="WebApplication2.netToWebFantion" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function webfunction(par) {
alert("后端调用前端的方法:"+ par);
return false;
}
</script>
<title>c#后端调用前端的方法</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="net"/> </div>
</form>
</body>
</html>
后端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApplication2
{
public partial class netToWebFantion : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void net(object sender, EventArgs e) {
ClientScript.RegisterStartupScript(this.GetType(), "par", "<script>webfunction('你好吗!')</script>");//webfunction('par')是前台jquery函数
} protected void Button1_Click(object sender, EventArgs e)
{ }
}
}
核心的代码就是后端:ClientScript.RegisterStartupScript(
this
.GetType(),
"par"
,
"<script>webfunction('你好吗!')</script>"
);
最新文章
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(70)-微信公众平台开发-成为开发者
- HTTP的长连接和短连接——Node上的测试
- typeof的一些兼容性问题
- vim 编辑器使用积累(for win)
- Java dynamical proxy demo
- Js获取当前日期时间及其它操作(转)
- POJ1046Color Me Less
- ArcObjects SDK(AE)10.1在vs2012安装的方法
- angular中的orderBy过滤器使用
- 使用EditText搜索listview里面的内容,实现Listview跟随变动的情况
- android隐式intent使用场景解析
- listview,fragment结合起来
- SIX GOD
- Fix a Tree
- Java数据持久层框架 MyBatis之API学习三(XML 映射配置文件)
- Linux二进制分析PDF
- Mongo安装与使用
- 转 WiFi的STA和AP模式指什么?
- Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力
- 使用 SSH 和 SFTP 协议