C#(ASP.NET)后端调用前端的方法 - code:9527 -- 博客

前端代码:

<%@ 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(test) {
            alert("后端调用前端的方法:"+ test);
            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(), "test", "<script>webfunction('are you ok!')</script>");//webfunction('par')是前台jquery函数
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
 
        }
    }
}

主要的核心代码就是后端:ClientScript.RegisterStartupScript(this.GetType(), “test”, “”);

喜欢博主的可以点击关注加一键三连,各种ASP.NET系统的源码已上传到主页资源中,欢迎下载

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code:9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值