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
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" />