【问题标题】:Render the content of a TemplateField dynamically动态渲染 TemplateField 的内容
【发布时间】:2011-05-12 12:43:26
【问题描述】:

我想知道是否有一种方法可以从 GridView 动态呈现模板字段的内容。

这就是网格的外观和我想要的,以某种方式在后面的代码中获取标签的渲染字符串。

<asp:GridView runat="server" ID="simpleGrid" AutoGenerateColumns="false" Visible="false">           
        <Columns>
            <asp:TemplateField HeaderText="Templated Date">
                <ItemTemplate>
                    <asp:Label ID="firstLabel" Text='<%# Eval("Date") %>' runat="server"/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

提前致谢, 卡利。

【问题讨论】:

  • 你是说你想要Label控件的HTML?
  • 我在问你是否想要这样做,或者我是否误解了这个问题。所以这就是你想要做的,还是别的什么?
  • 是的...我想从 TemplateField 中获取渲染标签的 HTML。

标签: asp.net gridview


【解决方案1】:

嗯,获取控件内容的唯一方法是使用 RenderControl 方法,通过以下方式:

StringWriter strings = new StringWriter();
HtmlTextWriter html = new HtmlTextWriter(strings);

Label label = //find the reference to the label
label.RenderControl(html);

这应该将控件的标记推送到 html 编写器中,并通过字符串编写器轻松提取。这是一种方式。否则,除了在客户端 javascript 中,没有直接的方法可以访问其 HTML。

HTH。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    相关资源
    最近更新 更多