Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with response.write + javascript 1

Status
Not open for further replies.

dace

Programmer
Jul 21, 2001
263
US
Hi everyone-

I'm using response.write in an asp.net codebehind to add some javascript to the http response:

Response.Write("<script language=" & Chr(34) & "javascript" & Chr(34) & "> document.getElementById('PageContentFrame').src='WebForm3.aspx' </script>")

Basically, I want this to set the contents of an iframe. However, it appears that when this javascript fires, the page hasn't been rendered yet, so there's no object there for it to set the location for. I don't know when/where the response.write goes.

Does anyone have any suggestions about how to get around this?

Thanks!
 
Not sure, but I believe the preferred way to do this is to use the Page.RegisterStartupScript() method. You would just build up your script in a string and call that method.
Code:
Imports System.Text
...

Dim script as StringBuilder = new StringBuilder()
With script
.Append("<script language=")
.Append(Chr(34))
.Append("javascript")
.Append(Chr(34))
.Append("document.getElementById('PageContentFrame').src='WebForm3.aspx'</script>")
End With

Page.RegisterStartupScript(script.ToString())

script = nothing


Greetings,
Dragonwell
 
Wow, that's exactly what I needed! Thanks a lot.


As a sidenote, if anyone else is ever looking to do this, I got it to work by declaring the iframe as a server-side control of type System.Web.UI.HtmlControls.HtmlGenericControl, then using:

PageContentFrame.Attributes.Add("src", "newpage.html")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top