response.write();改变页面布局的原因及解法

 

Response.Write ,在ASP.NET 中不要随便使用 Response.Write,其原因是它会打乱 ASP.NET 的输出流顺序,在aspx.cs 中使用 Response.Write 的输出会出现在页面的最顶部。会先于CSS执行,会导致页面布局得不到预期效果。response.write用来做做测试还勉强,正式运行就是在不行。

解法:Page.ClientScript.RegisterStartupScript(this.GetType(),"alert","<script>alert('保存成功');</script>");

示例:

Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功!');window.location.href='EqpInfo.aspx';</script>");

另一种解决办法:

在页面中加一个<asp:Literal ID="Literal1" runat="server"></asp:Literal>控件,把代码赋给它也可以。当然,也可以赋值给lable,只是能不用lable的地方就不用,都用literal代替。

原文地址:https://www.cnblogs.com/zhoumo/p/3201996.html