在ASMX Web服务中使用Server.Execute()呈现UserControl的问题

时间:2009-06-10 15:57:20

标签: c# asp.net user-controls asmx

任何人都可以解释为什么Server.Execute()要求我呈现的UserControls包含<form>标签(或者我正在做错的是使我的UserControls中的Server.Execute()需要表单标签) ?

我创建了一个ASMX服务,通过JQuery + JSON动态加载UserControl,如下所示:

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将控件加载到页面中,用来自服务返回的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>

这适用于一个巨大的警告。如果我没有在.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控件添加一个表单标签以解决此问题时,表单将呈现,但渲染器会重写控件中的表单标记,以便POST回ASMX服务而不是aspx中定义的表单页。

我用Google搜索并发现了Scott Guthrie的优秀ViewManager例子。我没有看到与他在那里所做的任何根本不同的事情,这让我相信我正在做的事情应该起作用。

5 个答案:

答案 0 :(得分:21)

看起来答案已隐藏在ViewManager

的评论中

您需要一个继承自Page的类,并覆盖不在表单中的服务器控件检查

public class FormlessPage : Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}

然后在渲染控件时,使用

Page page = new FormlessPage();
UserControl ctl = (UserControl)page.LoadControl(controlName);
//etc

我假设你将失去从这种方式渲染的任何控件中发射事件的能力。

答案 1 :(得分:1)

不要在用户控件上使用asp.net隐藏控件,只需使用带有代码标记的常规html隐藏输入&lt; %%&gt;填写这样的数据:

<input id="Hidden1"  type="hidden" value="<%= text %>"/>

“text”是代码隐藏文件中的公共变量。

这对我有用,并且不需要带有runat="server"的表单。

答案 2 :(得分:1)

<System.Web.Services.WebMethod()> _
Public Shared Function GetDetails(ByVal filename As String) As String
    Dim page As Page = New Page()
    Dim ctl As recDetails = CType(page.LoadControl("~/Controles/recDetails.ascx"), recDetails)
    ctl.FileName = filename

    page.EnableEventValidation = False
    Dim _form As New HtmlForm()
    page.Controls.Add(_form)
    _form.Controls.Add(ctl)

    Dim writer As New System.IO.StringWriter()
    HttpContext.Current.Server.Execute(page, writer, False)
    Dim output As String = writer.ToString()
    writer.Close()
    Return output
End Function

您动态添加表单

答案 3 :(得分:0)

您可以按如下方式更改GetControl()方法:

private string GetControl(String controlName, String ClassId)
{
    Page page = new Page();
    StringWriter writer = new StringWriter();
    page.Server.Execute(controlName, writer, false);
    return writer.ToString();
}

答案 4 :(得分:0)

    <System.Web.Services.WebMethod()> _
Public Shared Function GetDetails(ByVal filename As String) As String
    Dim page As Page = New Page()
    Dim ctl As recDetails = CType(page.LoadControl("~/Controles/recDetails.ascx"), recDetails)
    ctl.FileName = filename

    page.EnableEventValidation = False
    Dim _form As New HtmlForm()
    page.Controls.Add(_form)
    _form.Controls.Add(ctl)

    Dim writer As New System.IO.StringWriter()
    HttpContext.Current.Server.Execute(page, writer, False)
    Dim output As String = writer.ToString()
    writer.Close()
    Return output
End Function

您动态添加表单