您可以通过使用简单的 .asmx 页面而不是代码隐藏页面来避免静态约束。
1) 使用 AJAX Enable ASP.NET 模板打开新网站(它将必要的引用放在 web.config 中)
2) SIMPLESERVICE.ASMX - 添加一个新的 .asmx Web 服务(我叫我的 SimpleService.asmx) 注意 [System.Web.Script.Services.ScriptSerive] 装饰和 SimpleService 类实现 Web 服务。
<%@ WebService Language="C#" Class="SimpleService" %>
using System;
using System.Web.Services;
[System.Web.Script.Services.ScriptService]
public class SimpleService : WebService
{
[WebMethod]
public string GetMessage(string name)
{
return "Hello <strong>" + name + "</strong>, the time here is: " + DateTime.Now.ToShortTimeString();
}
}
3) DEFAULT.ASPX - 要使用它,请在脚本管理器中引用该服务,并且您已关闭并正在运行。在我的 Javascript 中,我调用了 class.method - SimpleService.GetMessage。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function callServer() {
SimpleService.GetMessage($get("Name").value, displayMessageCallback);
}
function displayMessageCallback(result) {
$get("message").innerHTML = result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Services>
<asp:ServiceReference Path="~/SimpleService.asmx" />
</Services>
</asp:ScriptManager>
<div>
</div>
<h1>Hello World Example</h1>
<div>
Enter Name: <input id="Name" type="text" />
<a href="javascript:callServer()">Call Server</a>
<div id="message"></div>
</div>
</form>
</body>
</html>
我使用了从 Scott Gu
Found Here 中找到的示例。