Hi,
I want to know to get content(in IE "View->Source" that produce current page content) of current aspx page in asp.net..
2 Replies - 2176 Views - Last Post: 03 June 2010 - 08:59 AM
Replies To: How to get content of current aspx page in asp.net
#2
Re: How to get content of current aspx page in asp.net
Posted 02 June 2010 - 05:25 AM
So you want to get the source of your own page?
this should work..
not tested.
this should work..
WebClient client = new WebClient();
string source = client.DownloadString("yourURL");
not tested.
#3
Re: How to get content of current aspx page in asp.net
Posted 03 June 2010 - 08:59 AM
I thought that you could use the RenderControl() method and so 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:
Notice, though, that I did not call the RenderControl method for the Page....
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.
-Frinny
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:
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());
}
Notice, though, that I did not call the RenderControl method for the Page....
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.
-Frinny
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|