Fork me on GitHub

WebForm后端调用前端JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*注意:定义的js方法一定要放在head里面,不能放下面,否则不生效*/<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormBackEndCallFrontJsDemo.WebForm1" %>
 
<!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 showInfo() {
            alert("Hello World");
        }
    </script>
    <title>WebForm后端调用前端JS</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="方法1" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="方法2" OnClick="Button2_Click" />
    </form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Web.UI;
 
namespace WebFormBackEndCallFrontJsDemo
{
    /// <summary>
    ///     ASP.NET在后台执行前台的js代码
    ///     LDH @ 2022-1-27
    /// </summary>
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) Response.Write("<script>alert('Welcome to study asp.net.');</script>");
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            //  此方法系统会提示已过时,不用管它,一切都正常使用
            Page.RegisterClientScriptBlock("MyScript", "<script defer>javascript:showInfo();</script>");
        }
 
        protected void Button2_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "<script defer>javascript:showInfo();</script>");
        }
    }
}

  转至:WebForm后端调用前端JS - Love In Winter - 博客园 (cnblogs.com)

posted @   WantRemake  阅读(215)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示