前端代码:
<%@ 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”, “”);
本文介绍了一个使用C#后端调用前端JavaScript函数的例子。通过ASP.NET平台,利用ClientScript.RegisterStartupScript方法实现跨语言调用。前端页面包含一个按钮,点击后触发后端代码,再由后端调用前端的webfunction函数。
后端调用前端的方法 - code:9527 -- 博客&spm=1001.2101.3001.5002&articleId=114371445&d=1&t=3&u=b411aa1900db4586a0bddf912a0aa3d0)
807

被折叠的 条评论
为什么被折叠?



