zoukankan      html  css  js  c++  java
  • C#后端调用前端的方法

    在我实际开发过程中,刚好遇到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>");

  • 相关阅读:
    别折腾自己
    在线工具集合
    搜索新技能
    saml login的流程
    给学网 好网站
    drush .. drupal console
    地铁健身操
    js扩展符号
    for in | for in 比较 解释 | 以后找知识点先从这里面搜索
    grep 查找字符串 在文件或者文件夹中
  • 原文地址:https://www.cnblogs.com/feipengting/p/9056766.html
Copyright © 2011-2022 走看看