做页面时,遇到了在后台调用Response.Writer(“<script>alert('Message')</script>”);弹出框后页面的(css)布局发生了变化,于是上网上搜了一些资料,总结下来,防止以后再用。
主要是使用了RegisterClientScriptBlock类,也可以使用RegisterStartupScript(二者的区别可以此页或这页 再或者MSDN),下面只把代码给出(也是从网上陶的具体位置在此)
代码如下:
调用下面的方法弹出提示对话框后会出现css失效
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
解决方法代码如下:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// 弹出对话框(不影响css样式)
///
/// 页面指针,一般为this
/// scriptKey为脚本键,唯一比如onclick或者其他的方法名等等
/// 提示信息
public static void ShowMessage(System.Web.UI.Page page, string scriptKey, string message)
{
System.Web.UI.ClientScriptManager csm = page.ClientScript;
if (!csm.IsClientScriptBlockRegistered(scriptKey))
{
csm.RegisterStartupScript(Page.GetType(), scriptKey, "<script>alert('" + message + "')</script>");
// 或者下面这句,好像都行
//ClientScript.RegisterClientScriptBlock(this.GetType(), "onclick", "<script>alert('dfds')</script>");
}
}
// 例如:如果是用在按钮的onclick()上则可以这样用
public static void ShowMessage(string message)
{
System.Web.UI.ClientScriptManager csm = page.ClientScript;
if (!csm.IsClientScriptBlockRegistered("onclick"))
{
csm.RegisterStartupScript(Page.GetType(), "onclick", "<script>alert('" + message + "')</script>");
// 或者下面这句,好像都行
//ClientScript.RegisterClientScriptBlock(this.GetType(), "onclick", "<script>alert('dfds')</script>");
}
}
以上都是在网上找的,本来是在园子里的一篇文章,可是现在没有了,只好从别处弄来了!