Response.Write("JS Code")后页面样式丢失问题【转】

转:http://yotoamo.blog.163.com/blog/static/8327765720091201038648/

VS2008中的“Alert”破坏网页样式的解决方法

学习滋味 2009-02-20 01:00:38 阅读47 评论0 字号:

        

这两天网站,遇到了输入JS脚本,破坏CSS样式的问题,原来写法:

Response.Write("<script>alert('请先保存修改内容!');</script>");                  破坏网页样式

我的笨方法:

在网页里加一个隐藏的label(其实就是字体颜色与背景一样),写成:

this.lblerror.Text = "<script language='javascript'>alert('请先保存修改内容!');</script>";

网友建议:

1、去掉language='javascript' ,问题依旧

2、后面加上Response.Write("<script>document.location=document.location;</script>"); 这句话,试了一下,发现样式是没变化,但是页面是从新加载的,VIEWSTATE被清空了(这可不行,用户修改还没保存呢)

3、this.RegisterStartupScript("", "<script language=javascript>alert('请先保存修改内容!')</script>");

代替你的

Response.Write("<script language='javascript'>alert('请先保存修改内容!');</script>");

最好方法:

用RegisterStartupScript是向客户端输出脚本,比Response.Write好在把脚本输入在网页最下面,不会影响样式,但是有时VS2005会提示一个长长的警告,解决方法是写成:

ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('请先保存修改内容!')</script>");

就OK了!(至于为什么,我也不太明白!)

参考:

http://zhidao.baidu.com/question/72404717.html?quesup1

http://hi.baidu.com/%B3%D4%CA%B2%C3%B4%C4%DC%B3%A4%C5%D6/blog/item/899d6b1224d5ba53f919b8df.html

posted @ 2010-06-02 18:20  lp123  阅读(388)  评论(0编辑  收藏  举报