JavaScript messes up aspx page (2.0) CSS formatting

S

sck10

Hello,

I have a page with an ImageButton that is used to redirect to another page.
When the page first opens, everything looks as expected. However, when I
click on the image, the new page opens as expected. However, when I go back
to the original page, all the font sizes are larger. Its as if by clicking
on the ImageButton, my CSS formatting was discarded.

When I look at the source for the following code, this is what I get:

<script language="JavaScript" type="text/JavaScript">
window.open("srcfinance.aspx?qv01=14&print=printme", "_blank")
</script>


Sub Print_Click(ByVal Src As Object, ByVal Args As CommandEventArgs)

Dim strTarget As String = _
"<script language=" & strQuote & "javascript" & strQuote & _
" type=" & strQuote & "text/JavaScript" & strQuote & ">" & _
"window.open(" & strQuote & "srcfinance.aspx?qv01=" & _
Me.hdnQV01.Value & "&print=printme" & strQuote & ", " & _
strQuote & "_blank" & strQuote & ")</script>"

Response.Write(strTarget)

End Sub
 
S

Steven Cheng[MSFT]

Hello Sck10,

Thank you for posting in ASPNET newsgroup.

As for the ASP.NET page css formatting issue, I've performed some local
test according to your description and I did manage to reproduce the same
behavior(when the new page opened, original page's css formatting get
lost...).

And based on my further test, I think the problem is caused by the approach
you used to register the client-script. In your page, you use
"Response.Write" to output client-script, this is doable, however, not
recommended because "Response.Write" will always output the string/text
before the page's html content, this will make the page output not a valid
html/xhtml document(I think you found the output script block at the top of
the page source when viewing the source in client browser, correct?).
Though IE or other popular browser can correct parse this, this is not
always guaranteed to work. And in this case, the cause the css format not
correctly get resolved.

In stead of using "Response.Write", you can use the "ClientScriptManager"
class provided in asp.net 2.0, you can get it through the Page class's
"ClientScript" property:

#Page.ClientScript Property
http://msdn2.microsoft.com/en-us/library/system.web.ui.page.clientscript.asp
x

e.g.
===========================
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim str As String

str = "<script language=""javascript""
window.open(""PrintPage.aspx?qv01=14&print=printme"",
""_blank"");</script>"

Page.ClientScript.RegisterStartupScript(Me.GetType(),
"print_script", str)

End Sub

=======================

the clientscriptmanager.xxxx method can ensure those script blocks to be
registered in the proper location in the page body. Also, I've performed
local test and after changed to use the ClientScriptManager( to register
script), the css formatting issue went away.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Steven,

Have you got any any progress on this issue or does my last reply help you
a little? If there is still anything we can help, please feel free to post
here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

sck10

Hi Steve,

I'm sorry, but you are referring to your last reply, which my reader is not
showing. Actually, its showing your reply below as the only reply. To
answer your question, no, I haven't made any progress on this issue...
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

sck10 said:
Hi Steve,

I'm sorry, but you are referring to your last reply, which my reader is not
showing. Actually, its showing your reply below as the only reply. To
answer your question, no, I haven't made any progress on this issue...

Here is a re-post of Stevens first reply:



Hello Sck10,

Thank you for posting in ASPNET newsgroup.

As for the ASP.NET page css formatting issue, I've performed some local
test according to your description and I did manage to reproduce the same
behavior(when the new page opened, original page's css formatting get
lost...).

And based on my further test, I think the problem is caused by the approach
you used to register the client-script. In your page, you use
"Response.Write" to output client-script, this is doable, however, not
recommended because "Response.Write" will always output the string/text
before the page's html content, this will make the page output not a valid
html/xhtml document(I think you found the output script block at the top of
the page source when viewing the source in client browser, correct?).
Though IE or other popular browser can correct parse this, this is not
always guaranteed to work. And in this case, the cause the css format not
correctly get resolved.

In stead of using "Response.Write", you can use the "ClientScriptManager"
class provided in asp.net 2.0, you can get it through the Page class's
"ClientScript" property:

#Page.ClientScript Property
http://msdn2.microsoft.com/en-us/library/system.web.ui.page.clientscript.asp
x

e.g.
===========================
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim str As String

str = "<script language=""javascript""""_blank"");</script>"

Page.ClientScript.RegisterStartupScript(Me.GetType(),
"print_script", str)

End Sub

=======================

the clientscriptmanager.xxxx method can ensure those script blocks to be
registered in the proper location in the page body. Also, I've performed
local test and after changed to use the ClientScriptManager( to register
script), the css formatting issue went away.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Thank you for your help Guffa,

Hi Sck10,

Guffa has helped repost my first response. Please feel free to let me know
if you still have any problem on accessing it.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

sck10

Thanks Göran. Worked great...


Göran Andersson said:
Here is a re-post of Stevens first reply:



Hello Sck10,

Thank you for posting in ASPNET newsgroup.

As for the ASP.NET page css formatting issue, I've performed some local
test according to your description and I did manage to reproduce the same
behavior(when the new page opened, original page's css formatting get
lost...).

And based on my further test, I think the problem is caused by the
approach
you used to register the client-script. In your page, you use
"Response.Write" to output client-script, this is doable, however, not
recommended because "Response.Write" will always output the string/text
before the page's html content, this will make the page output not a valid
html/xhtml document(I think you found the output script block at the top
of
the page source when viewing the source in client browser, correct?).
Though IE or other popular browser can correct parse this, this is not
always guaranteed to work. And in this case, the cause the css format not
correctly get resolved.

In stead of using "Response.Write", you can use the "ClientScriptManager"
class provided in asp.net 2.0, you can get it through the Page class's
"ClientScript" property:

#Page.ClientScript Property
http://msdn2.microsoft.com/en-us/library/system.web.ui.page.clientscript.asp
x

e.g.
===========================
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim str As String

str = "<script language=""javascript""
""_blank"");</script>"

Page.ClientScript.RegisterStartupScript(Me.GetType(),
"print_script", str)

End Sub

=======================

the clientscriptmanager.xxxx method can ensure those script blocks to be
registered in the proper location in the page body. Also, I've performed
local test and after changed to use the ClientScriptManager( to register
script), the css formatting issue went away.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no
rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

You're welcome :)

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top