Jef*_*ard 15 c# asp.net user-controls asmx
任何人都可以解释为什么Server.Execute()要求我呈现的UserControls包含<form>标签(或者,我正在做错的是使Server.Execute()在我的UserControls中需要表单标签)?
我创建了一个ASMX服务,通过JQuery + JSON动态加载UserControls,如下所示:
ControlService.asmx
<%@ WebService Language="C#" CodeBehind="ControlService.asmx.cs" Class="ManagementConcepts.WebServices.ControlService" %>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我通过以下JQuery将控件加载到一个页面中,用来自服务返回的HTML替换id为ContentPlaceholder:
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>
Run Code Online (Sandbox Code Playgroud)
这适用于一个巨大的警告.如果我没有在.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"/>
Run Code Online (Sandbox Code Playgroud)
当我将一个表单标记添加到ascx控件来解决此问题时,表单将呈现,但渲染器会重写控件中的表单标记,以便它回发到ASMX服务而不是aspx页面中定义的表单.
我用Google搜索并发现了Scott Guthrie出色的ViewManager示例.我没有看到与他在那里所做的任何根本不同的事情,这让我相信我正在做的事情应该起作用.
Bel*_*ela 21
看起来答案隐藏在ViewManager的注释中
您将需要一个继承自Page的类,并覆盖不在表单中的服务器控件的检查
public class FormlessPage : Page
{
public override void VerifyRenderingInServerForm(Control control)
{
}
}
Run Code Online (Sandbox Code Playgroud)
然后在渲染控件时使用
Page page = new FormlessPage();
UserControl ctl = (UserControl)page.LoadControl(controlName);
//etc
Run Code Online (Sandbox Code Playgroud)
我假设你将失去从任何以这种方式呈现的控件触发事件的能力.