response.write输出 改变页面的结构

用response.write输出东西了。输出的东西在页面的最上面,比如
....
<Doctype ...>
<html> ..
也就是在页面的前面加了东西,这样就破坏了页面的结构,感觉上,css也就出问题了。呵呵
事实上是response.write打乱了页面的结构所致,解决方案如下:
方法0:换成客户端脚本程序

Page.RegisterStartupScript("ssss", "<script>alert('图片格式不正确')</script>");

说明:ssss相当一个标识ID,只要不重覆就可以

方法一:把 Response.Write()语句替换为这个
Page.RegisterStartupScript("ServiceManHistoryButtonClick", "<script>window.open('EquipmentHistory.aspx?eid=" + ServiceManDropDownList.SelectedValue + "');</script>");
这个方法用于在页响应中发出客户端脚本块,前一个参数是该Script在页面中的唯一名称(随便起,不重复就行),后一个是脚本内容。
这个方法应该是微软官方推荐的方法。

方法二:在原程序的Response.Write()语句后再加一句
Response.Write("<script>document.location=document.location;</script>");
 
 
 
======================================================================

解决:使用RegisterClientScriptBlock或RegisterStartupScript方法。

namespace Common
{
    /// <summary>
    /// 提示信息
    /// </summary>
    public class MessageBox
    {
        public static void Alert(Page page,string message)
        {
            if (!page.ClientScript.IsClientScriptBlockRegistered("demo"))
            {
        //HttpContext.Current.Response.Write("<script>document.location=document.location;alert('" +message + "');</script>");网上有人使用这种解决方法,但重设document.location会重新加载页面。
                  page.ClientScript.RegisterClientScriptBlock(page.GetType(), "demo", "<script>alert('" + message +"');</script>");
            }
        }
    }
}

使用:

Common.MessageBox.Alert(this,"每位学生只能选择一个题目!");

DEMO,查看几种方法输出的位置区别:

后台文件,Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write("<script>alert('Hello'');</script>");

        //this.ClientScript.RegisterStartupScript(this.GetType(), "demo", "<script>alert('" + "Hello" + "');</script>");
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "demo", "<script>alert('Hello'');</script>");
    }
}

输出的页面:

Response.Write输出在页面内容上方。

<script>alert('Hello');</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
    <div>
      <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"   

        value="/wEPDwULLTE2MTY2ODcyMjlkZAVpRcHibkbkAXYhTouZnL6SNJ8d" />
    </div>

      <div>

      </div>
    </form>
</body>
</html>

RegisterStartupScript输出在ASP.NET页面底部,关闭元素</form>之前。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
  <form name="form1" method="post" action="Default.aspx" id="form1">
    <div>
      <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"

        value="/wEPDwULLTE2MTY2ODcyMjlkZAVpRcHibkbkAXYhTouZnL6SNJ8d" />
    </div>

      <div>
      </div>
    <script>alert('Hello');</script>

  </form>
</body>
</html>

RegisterClientScriptBlock输出在ASP.NET页面中开启元素<form>之后。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
    <div>
      <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"

        value="/wEPDwULLTE2MTY2ODcyMjlkZAVpRcHibkbkAXYhTouZnL6SNJ8d" />
    </div>

    <script>alert('Hello');</script>
      <div>
      </div>
    </form>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值