Render the content of a TemplateField dynamically
I want to know if there is a way of dynamically render the content of a template field from a GridView.
This is how the grid looks and what I want, is to somehow get the rendered string of the label in the code behind.
<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>
Thanks in advance, Kali.
Well, the only way to get the content of a control is to use the RenderControl method, via something like:
StringWriter strings = new StringWriter();
HtmlTextWriter html = new HtmlTextWriter(strings);
Label label = //find the reference to the label
label.RenderControl(html);
This should push the control's markup into the html writer and easily extracted through the string writer. That's one way. Otherwise, no direct way to access its HTML except in client-side javascript.
HTH.
精彩评论