【发布时间】:2010-11-01 20:51:20
【问题描述】:
谁能解释为什么 Server.Execute() 要求我渲染的 UserControls 包含 <form> 标签(或者,我做错了什么,使 Server.Execute() 在我的 UserControls 中需要表单标签)?
我已经创建了一个 ASMX 服务来通过 JQuery+JSON 动态加载 UserControls,如下所示:
ControlService.asmx
<%@ WebService Language="C#" CodeBehind="ControlService.asmx.cs" Class="ManagementConcepts.WebServices.ControlService" %>
ControlService.cs
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ControlService : System.Web.Services.WebService
{
private string GetControl(String controlName, String ClassId)
{
Page page = new Page();
UserControl ctl = (UserControl)page.LoadControl(controlName);
page.Controls.Add(ctl);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
return writer.ToString();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetSimpleControl(string ClassId)
{
return GetControl("SimpleControl.ascx", ClassId);
}
}
我通过以下 JQuery 将控件加载到页面中,该 JQuery 将带有 ID ContentPlaceholder 的 a 替换为从服务返回的 HTML:
JQueryControlLoadExample.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JQueryControlLoadExample.aspx.cs" Inherits="ControlService_Prototype._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ControlService Prototype</title>
</head>
<body>
<form id="theForm" runat="server" action="JQueryControlLoadExample.aspx">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
<Scripts>
<asp:ScriptReference NotifyScriptLoaded="true" Path="~/Scripts/jquery-1.3.2.js" />
</Scripts>
</asp:ScriptManager>
<div>
<asp:HiddenField runat="server" ID="hdncourse"/>
<asp:HiddenField runat="server" ID="hdnTargetContent" Value="GetSimpleControl"/>
<div runat="server" id="ContentPlaceholder" class="loading"></div>
</div>
<script type="text/javascript">
$(document).ready(function() {
var servicemethod = document.getElementById("hdnTargetContent").value;
$.ajax({
type: "POST",
url: "ControlService.asmx/" + servicemethod,
data: "{'ClassId':'"+document.getElementById("hdncourse").value+"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#ContentPlaceholder').html(msg.d);
}
});
});
</script>
</form>
</body>
</html>
这有一个很大的警告。如果我没有在 .ascx 控件的标记中定义表单,那么 HttpContext.Current.Server.Execute() 会抛出带有以下消息的 HttpException:
Control 'hdnspecialoffer' of type 'HiddenField' must be placed inside a form tag with runat=server.
SimpleControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SimpleControl.ascx.cs" Inherits="ControlService_Prototype.UserControls.SimpleControl" %>
<asp:HiddenField runat="server" ID="hdnspecialoffer"/>
当我向 ascx 控件添加一个表单标签来解决这个问题时,表单会呈现,但呈现器会重写控件中的表单标签,以便它返回到 ASMX 服务,而不是在 aspx 中定义的表单页面。
我搜索了一下,发现了 Scott Guthrie 的出色 ViewManager 示例。我看不出任何与他在那里所做的根本不同的事情,这让我相信我所做的应该是有效的。
【问题讨论】:
标签: c# asp.net user-controls asmx