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?
Printable View
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?
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
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...
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.
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:
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.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
Then simply put that UserControl on any page you wish to apply that functionality.
I must spread some reputation around, but good reply nonetheless. Well, a little less. :afrog: