2 Replies - 3522 Views - Last Post: 03 June 2010 - 08:59 AM Rate Topic: -----

#1 visweswaran28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 29-October 09

How to get content of current aspx page in asp.net

Posted 01 June 2010 - 10:58 PM

Hi,

I want to know to get content(in IE "View->Source" that produce current page content) of current aspx page in asp.net..
Is This A Good Question/Topic? 0
  • +

Replies To: How to get content of current aspx page in asp.net

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

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..

WebClient client = new WebClient();
string source = client.DownloadString("yourURL");



not tested.
Was This Post Helpful? 0
  • +
  • -

#3 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

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:

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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1