Results 1 to 6 of 6

Thread: [RESOLVED] ASP.net get page source from Control

  1. #1

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Resolved [RESOLVED] ASP.net get page source from Control

    Hey quick question,

    i have a user control on my aspx page. is it possible for that control to obtain the html source of the aspx page its on?
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: ASP.net get page source from Control

    Sure,

    You can utilize the Response.Filter property to point to a stream. You can create the stream class itself, and have it perform voodoo magic on the response, or even write it to a file or database on your server if you wish.
    see here

  3. #3

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: ASP.net get page source from Control

    im a bit confused by that.

    what i am trying to do is the in my control, in the render method im trying to get the html code of the web page that my control is on.

    The reason being is that i wish to add this control to many pages so i can replace tokens on each page...
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: ASP.net get page source from Control

    Does it have to be done from the user control, could you not just create a class to do this as shown in the link?

    Or similarly, you should be doing this from a base class for the page rather than a control, because then the control isn't... exactly a control.

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: ASP.net get page source from Control

    It shouldn't be confusing so let me clear it up for you.

    The Response.Filter property accepts a Stream class which would enable you to replace tokens as you stated.

    The Response.OutputStream is where all controls' Render methods write to. The Filter is another stream you provide which the runtime will apply after ALL Controls and HttpModules have been processed for the request. It is the absolute last stop before the content gets delivered to the client.

    You can simply create a shallow user control with no sub-controls on it, and in the Page_Load method, simply assign the Response.Filter property to an instance of your class.

    That class has to be inherited from Stream. The link I gave shows you an example of using a simple String.Replace call to replace parts of the html in the Stream's Write method. You could easily use Regex patterns as well.

    If you're trying to replace asp.net tokens like <ASP:TEXTBOX> with something else, this won't work for you because it replaces the actual HTML output of those controls, the tokens are interpreted by the ASP.NET parser when a request comes in, and would be a far more difficult endeavor.

    Simply copy the linked code (the ReplaceHtml class) exactly, include it your project, and then add a new UserControl to your project. In the code-behind, in the Page_Load method, assign the Response.Filter property as follows:
    Code:
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load   
            Response.Filter = New ReplaceHTML(Response.Filter)   
        End Sub
    You can then tweak the Write method in the ReplaceHtml class you copied from the link to replace any part of tokens you wish to find.
    Then simply put that UserControl on any page you wish to apply that functionality.
    Last edited by nemaroller; Oct 13th, 2008 at 07:58 AM.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: ASP.net get page source from Control

    I must spread some reputation around, but good reply nonetheless. Well, a little less.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width