检索ASCX WebControl内的HtmlGenericControl内容 [英] Retrieve contents of HtmlGenericControl inside ASCX WebControl
问题描述
我有一个 HtmlGenericControl
这是一个简单的DIV,具有 runat = server
此控件嵌入在多个页面上的ASCX WebControl内。在Page_Load中,此元素由数据绑定到数据库数据的中继器填充,该数据库是特定页面。
我遇到的问题是ASCX WebControls似乎没有很容易读取自己的元素的内容。
到目前为止,这已经失败了:
如何在.NET(C#)中获取UserControl的HTML输出?
我正在寻找一种方法来获取HtmlGenericControl内容的按钮点击。我该怎么做?
简化上一个问题。 检索ASCX中特定元素的HTML
输入
ASPX代码背后
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
ASPX标记
%@ Page EnableEventValidation =false.....
< h3> 之后的用户控制代码
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);
}
用户控制标记
< div runat =serverid =generic>
< asp:Repeater runat =serverID =repeater>
< ItemTemplate>
<%#Eval(QuestionText)%>
< / ItemTemplate>
< / asp:Repeater>
< / div>
< br />
< asp:Button Text =获取内容ID =getContentrunat =serverOnClick =getContent_Click/>
< br />
< asp:Label ID =messagerunat =server/>
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" />
这篇关于检索ASCX WebControl内的HtmlGenericControl内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!