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>");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM