Retrieve contents of HtmlGenericControl inside ASCX WebControl

女生的网名这么多〃 提交于 2019-12-31 07:22:47

问题


I have an HtmlGenericControl which is a simple DIV with runat=server

This Control is embedded inside of an ASCX WebControl which resides on multiple pages. At Page_Load this element is populated by a repeater that is data-bound to database data that is Page Specific.

The trouble I'm having is ASCX WebControls don't seem to read the contents of their own elements very easily.

So far this has failed: How do I get the HTML output of a UserControl in .NET (C#)?

I'm looking for a way to get the contents of the HtmlGenericControl inside of a button click. How would I do that?

Simplifying previous question. Retrieve HTML of specific element in ASCX


回答1:


OK, I got it working (I think...)

Output

ASPX Code behind

    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

ASPX markup

%@ Page EnableEventValidation="false" .....

User control code behind

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            var d = new QuestionsContext().GetQuestions();

            this.repeater.DataSource = d;
            this.repeater.DataBind();
        }
    }

    protected void getContent_Click(object sender, EventArgs e)
    {
        var sb = new StringBuilder();

        this.generic.RenderControl(new HtmlTextWriter(new StringWriter(sb)));

        string s = sb.ToString();

        this.Trace.Warn(Server.HtmlEncode(s));
        this.message.Text = Server.HtmlEncode(s);
    }

User control markup

<div runat="server" id="generic">
    <asp:Repeater runat="server" ID="repeater" >
        <ItemTemplate>
            <%# Eval("QuestionText") %>
        </ItemTemplate>
    </asp:Repeater>
</div>

<br />
<asp:Button Text="Get content" ID="getContent" runat="server" OnClick="getContent_Click" />
<br />
<asp:Label ID="message" runat="server" />


来源:https://stackoverflow.com/questions/11622541/retrieve-contents-of-htmlgenericcontrol-inside-ascx-webcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!