....
<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>