I want to know to get content(in IE "View->Source" that produce current page content) of current aspx page in asp.net..
How to get content of current aspx page in asp.net
Collapse
X
-
Tags: None
-
Previously I answered this question by recommending that you check out the RenderControl() method. I just tried it out as practice.
It's a bit trickier than I first thought.
You cannot call the RenderControl() method for the Page outside of the Page's Render event (apparently). I was trying to call it in the Page PreRender event so that I could add the source output to a Label on the page.
The following code works...it retrieves the source code for a Panel and displays this source code into a Label control (named "Src") on the page:
Code:protected void Page_PreRender(object sender, EventArgs e) { Panel myPanel = new Panel(); myPanel.ID = "myPanel"; Label myLabel1 = new Label(); myLabel1.Text = "First Label"; myLabel1.ID = "myLabel1"; Label myLabel2 = new Label(); myLabel2.Text = "Second Label"; myLabel2.ID = "myLabel2"; Panel anotherPanel = new Panel(); anotherPanel.ID = "anotherPanel"; Label myLabel3 = new Label(); myLabel3.Text = "Third Label: inside another panel."; myLabel3.ID = "myLabel3"; anotherPanel.Controls.Add(myLabel3); myPanel.Controls.Add(myLabel1); myPanel.Controls.Add(myLabel2); myPanel.Controls.Add(anotherPanel); System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter tw = new System.IO.StringWriter(sb); HtmlTextWriter hw = new HtmlTextWriter(tw); myPanel.RenderControl(hw); Src.Text = Server.HtmlEncode(sb.ToString()); }
First of all you cannot call the RenderControl() method for the Page outside of the Render event for the page (so calling it in the PreRender event won't work). Secondly, I'm not sure if you can output the source code to two different sources...I started by trying to clone the Page using Reflection but this is where I ran into problems and decided to stop.
I'm not sure what you're planning on doing with the output and therefore can't determine whether or not this solution will even be viable for you.
Until you respond with more details I cannot help you further.
-FrinnyComment
Comment