2

I have an physically existing aspx file on IIS. It has a asp.net Grid View controls and few lines of html.

Now what I want is , the code will render the aspx somewhere in memory with the grid view populated with database in runtime. Then through my code , i will be able to read the entire generated html.

Is it possible? Or any alternative so that I can open the form but it will be not availabe to the user, some kind of visible=false thingy.

Kindly help.

Note:

I am expecting same process as we use to read txt files. But here i need one more extra stuffing, that is ,calling the page life cycle events too.

  • What do you mean, strip off the HTML? Don't you want the HTML? Or do you only want _some_ of the HTML? What will you do with the results of this page? – John Saunders Dec 12 '13 at 19:12
  • @JohnSaunders : I simple words after rendering the aspx in memory, I would like to read its entire html as normal text –  Dec 12 '13 at 19:13
  • OK, so you don't want to _strip_ the HTML, you want to _read_ the HTML. "Strip" means to remove parts of it, like you might have wanted to strip off the `` element for some reason. – John Saunders Dec 12 '13 at 19:17
  • @JohnSaunders sorry my mistake... question updated... –  Dec 12 '13 at 19:18
  • Also, which page lifecyle events do you need? You can get `Page_Load` and `DataBind`, but I don't know how much more. Certainly no postback events like `SelectedIndexChanged`. – John Saunders Dec 12 '13 at 19:20
  • @JohnSaunders: In more simpler words leave the other events, just guide me through the events during load. As my all codes will be in page load, I suupose –  Dec 12 '13 at 19:23
  • 1
    Can't u simple use `WebClient.DownloadString` ? Point it to your ASPX file and download entire HTML? – Yuriy Galanter Dec 12 '13 at 20:59

2 Answers2

1

You could use the RenderControl method to output a control to an HtmlTextWriter object.

using (var textWriter = new StringWriter())
using (var writer = new HtmlTextWriter(textWriter))
{
    yourControl.RenderControl(writer);
    var html = textWriter.ToString();
}
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • The question is for the full aspx page, not for a control only – Aristos Dec 12 '13 at 19:15
  • Page inherits from `Control`, so you could use the same method. – Jacob Dec 12 '13 at 19:16
  • @Aristos yes... actually I need grid binding too on that page –  Dec 12 '13 at 19:17
  • 1
    @Amit: just call DataBind on the page before rendering. Note that you won't have the rest of the page lifecycle, either, so no "click" event handling, for instance. – John Saunders Dec 12 '13 at 19:17
  • @JohnSaunders no buttons. actually , i need only databinding event but that might me for more than one grids/ repeaters –  Dec 12 '13 at 19:20
  • I believe that may be enough. I just looked at an example (sorry, can't post it, it's not mine), of a user control being rendered in that manner. It has an `` control within it, and the `DataBinding` event for that image is being handled. – John Saunders Dec 12 '13 at 19:23
  • @Amit: no, it belongs to my current employer. It's just "normal" code though: there's nothing special about it. That's why I think your page may "just work". – John Saunders Dec 12 '13 at 19:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43070/discussion-between-amit-ranjan-and-john-saunders) –  Dec 12 '13 at 19:25
  • @JohnSaunders no issues,, kindly take a look into chat –  Dec 12 '13 at 19:28
0

The scenario you are describing is not a very good one, from the design prospective. I must make clear that I would never suggest it, under normal circumstances. I suppose you have a very good reason you have to use this "hidden" page.

So this is my "suggestion". You could host the "hidden" aspx in a seperate web application in another process, not accessible from outside your server. Then, your normal asp.net application could make an HTTP request on code behind, targeting the "hidden" page and using the response (parsing the HTTP response using a tool like HTML Agility. You could hide the specific page by putting it on another site in IIS and secure it through a firewall rule. I am sure there are other ways to hide it too, but this is an easy way to go.

Hope I helped!

Pantelis Natsiavas
  • 5,293
  • 5
  • 21
  • 36