Hi I want to call the corresponding html inside a Panel in code behind. How can I do that?
I have this
<asp:Panel ID="MyPanel" runat="server">
// other asp.net controls and html stuffs here.
</asp:Panel>
I want to get the HTML equivalent of MyPanel
and all of its contents in my code behind say in PageLoad or some methods.
Thanks.
You have to add runat="server" tag to the HTML table and access that table by Id in Code behind. and then CAST it to HTMLTable.
If you use code-behind class files with . aspx pages, you can separate the presentation code from the core application logic (or code-behind). The code-behind class file is compiled so that it can be created and used as an object. This allows access to its properties, its methods, and its event handlers.
Does RenderControl() not work?
Create an instance of your control and then call RenderControl() on it. Of course this implies that your panel is in a UserControl
example from comments:
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
var html = sb.ToString();
@Shiv Kumar's answer is correct. However you don't need the StringBuilder
for this.
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
var html = tw.ToString();
This also works
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With