ASP.NET Ajax实现弹出提示框,页面变灰不可点击

最近在网上看到一篇文章,讲ASP.NET ajax中的异常处理,有一部分是自定义javascript处理异常。突然想到网易邮箱中,弹出对话框,后边的页面变灰且不可点击的效果。
在网上找了一下,实现方法就是用两个层,一个层用来显示提示信息,一个层用来遮住页面;还有一个办法就是用iframe.两者的不同之处大概就在于iframe可以遮住全部的页面元素,而div则不能遮住下拉列表。

我这个例子使用的div,绝大部分引用了:http://www.cnblogs.com/Terrylee/archive/2006/11/13/Customizing_Error_Handling.html

代码如下:
Default.aspx 前台页面及javascript

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Untitled Page</title>
  6. <style type="text/css">
  7. #UpdatePanel1{
  8. width: 200px; height: 50px;
  9. border: solid 1px gray;
  10. }
  11. #AlertDiv{
  12. left: 40%; top: 40%;
  13. position: absolute; width: 200px;
  14. padding: 12px;
  15. border: #000000 1px solid;
  16. background-color: white;
  17. text-align: left;
  18. visibility: hidden;
  19. z-index: 99;
  20. }
  21. #AlertButtons{
  22. position: absolute; right: 5%; bottom: 5%;
  23. }
  24. </style>
  25. </head>
  26. <body id="bodytag" style="margin: 0px">
  27. <form id="form1" runat="server">
  28. <asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError" />
  29. <script type="text/javascript">
  30. var divElem = 'AlertDiv';
  31. var messageElem = 'AlertMessage';
  32. var bodyTag = 'bodytag';
  33. var sWidth,sHeight;
  34. sWidth=document.body.offsetWidth;//浏览器工作区域内页面宽度
  35. sHeight=screen.height;//屏幕高度(垂直分辨率)
  36. //背景层(大小与窗口有效区域相同,即当弹出对话框时,背景显示为放射状透明灰色)
  37. var bgObj=document.createElement("div");//创建一个div对象(背景层)
  38. //定义div属性,即相当于
  39. // <div id="bgDiv" style="position:absolute; top:0; background-color:#777; filter:progid:DXImagesTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75); opacity:0.6; left:0; width:918px; height:768px; z-index:19999;"></div>
  40. bgObj.setAttribute('id','bgDiv');
  41. bgObj.style.position= "absolute";
  42. bgObj.style.display="none";
  43. bgObj.style.top= "0";
  44. bgObj.style.background= "#777";
  45. bgObj.style.filter= "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75)";
  46. bgObj.style.opacity= "0.6";
  47. bgObj.style.left= "0";
  48. bgObj.style.width=sWidth + "px";
  49. bgObj.style.height=sHeight + "px";
  50. bgObj.style.zIndex = "10000";
  51. $get(bodyTag).appendChild(bgObj);
  52. Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
  53. function ToggleAlertDiv(visString)
  54. {
  55. if (visString == 'hidden')
  56. {
  57. $get('bgDiv').style.display="none";
  58. $get(bodyTag).style.backgroundColor = 'white';
  59. }
  60. else
  61. {
  62. $get('bgDiv').style.display="";
  63. }
  64. var adiv = $get(divElem);
  65. adiv.style.visibility = visString;
  66. }
  67. function ClearErrorState() {
  68. $get(messageElem).innerHTML = '';
  69. ToggleAlertDiv('hidden');
  70. }
  71. function EndRequestHandler(sender, args)
  72. {
  73. if (args.get_error() != undefined)
  74. {
  75. var errorMessage;
  76. if (args.get_response().get_statusCode() == '200')
  77. {
  78. errorMessage = args.get_error().message;
  79. }
  80. else
  81. {
  82. errorMessage = 'An unspecified error occurred. ';
  83. }
  84. args.set_errorHandled(true);
  85. ToggleAlertDiv('visible');
  86. $get(messageElem).innerHTML = errorMessage;
  87. }
  88. }
  89. </script>
  90. <div>
  91. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  92. <ContentTemplate>
  93. <asp:TextBox ID="TextBox1" runat="server" Width="30px"></asp:TextBox>
  94. /<asp:TextBox ID="TextBox2" runat="server" Width="30px"></asp:TextBox>&nbsp;<asp:Button
  95. ID="Button1" runat="server" OnClick="Button1_Click" Text="Execute" />
  96. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  97. </ContentTemplate>
  98. </asp:UpdatePanel>
  99. <div id="AlertDiv" style="z-index: 20000;">
  100. <div id="AlertMessage">
  101. </div>
  102. <br />
  103. <div id="AlertButtons">
  104. <input id="OKButton" type="button" value="OK" runat="server" οnclick="ClearErrorState()" />
  105. </div>
  106. </div>
  107. </div>
  108. </form>
  109. </body>
  110. </html>

Default.aspx.cs 后台页面

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. try
  18. {
  19. int a = Int32.Parse(TextBox1.Text);
  20. int b = Int32.Parse(TextBox2.Text);
  21. int res = a / b;
  22. Label1.Text = res.ToString();
  23. }
  24. catch (Exception ex)
  25. {
  26. if (TextBox1.Text.Length > 0 && TextBox2.Text.Length > 0)
  27. {
  28. ex.Data["ExtraInfo"] = " You can't divide " +
  29. TextBox1.Text + " by " + TextBox2.Text + ".";
  30. }
  31. throw ex;
  32. }
  33. }
  34. protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
  35. {
  36. if (e.Exception.Data["ExtraInfo"] != null)
  37. {
  38. ScriptManager1.AsyncPostBackErrorMessage =
  39. e.Exception.Message +
  40. e.Exception.Data["ExtraInfo"].ToString();
  41. }
  42. else
  43. {
  44. ScriptManager1.AsyncPostBackErrorMessage =
  45. "An unspecified error occurred.";
  46. }
  47. }
  48. }

ok!本章到此结束!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值