Altering page content prior to rendering page on browser

R

Rob Heckart

Hi,

I'm using the Portal ASP.NET starter kit for a project. It uses one aspx
page, DesktopDefault.aspx to render content by loading different user
controls depending on what's clicked. One of my mandates is to have any
hyperlink on the page that points to a URL outside of our site pull up an
alert message. I have the javascript code, but what I want to know is, can I
see the entire HTML for the page prior to it being sent to the browser? Once
I see that, can I run through it in code and add the code to the URL and
then send the modified HTML to the browser? There's not too much written
about the Render methods and events page level.

Thanks!
Rob
 
W

wtsnet

What I'd probably do is write a Custom Control that
extends the hyperlink control you're using. Get it to
render the right Java 'popup' code to the client, and
embed the code to decide if the URL is internal or
external inside it. then just use your new control
instead of the hyperlink control. ASP.NET should render
it transparently. I've never done it mind (I'm ASP.NET
green), but that'd be my direction for a nice OO solution.

Alternatively, see if you can look at the Page.Response
object in one of the page Events? (Page_PreRender? No
idea!). You'll probably have to make sure the server
saves up the response for one big squirt rather than
return it in bits and bobs, so look at the
Page.BufferOutput Page.SuppressContent properties?

Check MSDN for more info.
 
R

Rob Heckart

I can't do it hyperlink level, because there are some controls that push out
a new article with a hyperlink embedded in a large section of text. The only
way is to grab the page and alter before pushing out to the browser.

Rob
 
W

wtsnet

Gotcha, Rob. What a pain!

It's probably easier to go the way you say, but...

could you design a container Custom Control, trap and
process the rendered output of it's children (ie the
difficult controls you mentionned) by calling
RenderChildren in the Sub Render of your Custom Control,
then pass the doctored output back in the htmlTextWriter
parameter of Render?

Sorry! I'll leave you alone and get back to my own
traumas! Be interested to hear how you get it going
though. Good luck!

Andy
 
R

Rob Heckart

Actually, just discovered a way to do it. Page level, do this:

' Make it so that any external link on the page gets the external
link confirm
' Javascript code added to it.
Dim sOutput As String
Dim nNextOccur As Integer = 0
Dim nFoundAt, nEndElement As Integer
Dim tempWriter = New StringWriter
Dim sURL As String

' Render to a temporary location
MyBase.Render(New HtmlTextWriter(tempWriter))
sOutput = tempWriter.ToString()
Dim sOutputLower As String = sOutput.ToLower ' Some anchors are
upper case - convert to catch all

Try

nFoundAt = sOutputLower.IndexOf("<a ", nNextOccur) ' Find a
tag

Do While nFoundAt <> -1

' Find the end of the anchor
nEndElement = sOutputLower.IndexOf(">", nFoundAt + 1)
sURL = sOutputLower.Substring(nFoundAt, nEndElement -
nFoundAt) ' Get the whole URL

' Add text to both strings to if there is no faa.gov in
the external URL
If sURL.IndexOf("http://") <> -1 And sURL.IndexOf("{Your
Domain here}") = -1 Then
sOutput = sOutput.Insert(nFoundAt + 3,
"onclick=""return confirm('You are leaving the {Whatever} web. The next site
you visit may have a different Privacy Policy. You may want to view the
Privacy Policy at the next site. Click OK to continue.')"" ")
sOutputLower = sOutputLower.Insert(nFoundAt + 3,
"onclick=""return confirm('You are leaving the {Whatever} web. The next site
you visit may have a different Privacy Policy. You may want to view the
Privacy Policy at the next site. Click OK to continue.')"" ")
End If

' Skip over the current anchor and look for the next one
nNextOccur = nEndElement + 1
nFoundAt = sOutputLower.IndexOf("<a ", nNextOccur)

Loop
Catch ex As Exception
' TODO: Put some exception code handling in here
End Try

' Write the final page output
writer.Write(sOutput)

See ya!
Rob
 
R

Rob Heckart

On my application, the user stays on the DesktopDefault.aspx page, and user
controls are dynamically loaded into its different placeholders. I overroad
the Render event for the aspx page and put the code there. Works like a
charm, and the user doesn't need to worry about putting the code in
everything themselves!

Rob
Gotta love .NET!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top