DataBind用户控件并返回HTML字符串

时间:2013-10-04 17:24:34

标签: c# data-binding webforms

我目前正在尝试从Web窗体用户控件中获取一些功能。我的想法是以类似于MVC的PartialView的方式使用它。我想要做的是从代码创建控件,与ASP Repeater服务器控件进行数据绑定,然后从方法返回呈现的HTML。

目前,我使用以下代码来定义用户控件:

<table id="comments">
    <thead>
        <tr>
            <th>Created On</th>
            <th>By</th>
            <th>Comment</th>
        </tr>
    </thead>
    <tbody>
        <asp:Repeater ID="commentsRepeater" runat="server">
            <ItemTemplate>
                <tr class="user-comment">
                    <td><%#Eval("ReviewedOn")%></td>
                    <td><%#Eval("Reviewer")%></td>
                    <td><%#Eval("Comment")%></td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </tbody>
</table>

如果我将数据绑定代码放在Load事件中,则不会发生任何事情。如果我将它放入方法并调用它,我会收到以下异常: Databinding methods such as Eval(), XPath(), and Bind() can only be used in controls contained in a page.

那么如何使用User控件来获得我想要的结果呢?

更新

这就是我目前尝试数据绑定的方式(这种方式不会绑定任何东西,但会返回周围的html字符串)

public partial class UserComments : System.Web.UI.UserControl
{
    public List<CallReview> Comments;

    protected void Page_Load(object sender, EventArgs e)
    {
        commentsRepeater.DataSource = Comments;
        commentsRepeater.DataBind();
    }
}

更新2

添加有关我如何尝试加载控件以获取HTML表示的代码。

//Will be used to load the control
var loader = new UserControl();
var ctl = (UserComments)loader.LoadControl("~/Controls/UserComments.ascx");

//Set the comments property of the control
ctl.Comments = _callAdapter.GetCallComments(callId);

//Create streams the control will be rendered to
TextWriter txtWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(txtWriter);

//Render the control and write it to the stream
ctl.RenderControl(writer);

//Return the HTML
return txtWriter.ToString();

1 个答案:

答案 0 :(得分:0)

好的,所以我想出了一个解决方法。我使用UserControl对象来渲染控件,而不是使用Page对象,因为它将正确呈现服务器控件。在此之后,我不得不将我的html代码包装在form runat="server"对象中。现在我没有遇到任何问题。

缺点是我仍然需要打电话给databind转发器,因为永远不会引发Load事件。

<强>控制:

<form runat="server">
<table id="user-comments">
    <thead>
        <tr>
            <th>Created On</th>
            <th>By</th>
            <th>Comment</th>
        </tr>
    </thead>
    <tbody>
        <asp:Repeater ID="commentsRepeater" runat="server">
            <ItemTemplate>
                <tr class="user-comment">
                    <td><%#Eval("ReviewedOn")%></td>
                    <td><%#Eval("Reviewer")%></td>
                    <td><%#Eval("Comment")%></td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </tbody>
</table>
</form>

<强>代码隐藏

    public partial class UserComments : System.Web.UI.UserControl
    {
        public List<CallReview> Comments;

        public void BindComments()
        {
            commentsRepeater.DataSource = Comments;
            commentsRepeater.DataBind();
        }
    }

呈现控件并获取原始HTML的代码:

//Will be used to load the control
var loader = new Page();
var ctl = (UserComments)loader.LoadControl("~/Controls/UserComments.ascx");

//Set the comments property of the control
ctl.Comments = _callAdapter.GetCallComments(callId);
ctl.BindComments();

//Create streams the control will be rendered to
TextWriter txtWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(txtWriter);

//Render the control and write it to the stream
ctl.RenderControl(writer);

//Return the HTML
return txtWriter.ToString();