asp.net 弹出div层

这是一项老技术了,但这几天在一个需要特殊于用户交互的地方用了一下,感觉不错,在这里和大家分享一下。

 

这项技术可以应用在,与用户进行的简单的需要用户输入的交互需求中。

 

1 、实现(客户端):即asp.net 网页的源中

1)CSS样式:

  1. body
  2. {
  3.     margin15px auto;
  4.     padding0px;
  5.     background-color#c3daf9;
  6.     text-aligncenter;
  7. }
  8. #header 
  9. {
  10.     bordersolid 1px #000;
  11.     padding10px;
  12.     margin0px;
  13.     width700px;
  14.     positionrelative
  15.     height20px;
  16.     text-aligncenter;
  17. }
  18. #header P 
  19. {
  20.     padding0px;
  21.     font-weightbold;
  22.     font-size24px;
  23.     margin0px;
  24.     color#2557ad;
  25. }
  26. #conten
  27. {
  28.     bordersolid 1px #000;
  29.     margin0px;
  30.     padding10px;
  31.     padding-top30px;
  32.     width700px;
  33.     height300px;
  34.     positionrelative;
  35.     background-color#fff;
  36.     text-alignleft;
  37. }
  38. .popupWindow
  39. {
  40.     position:absolute;
  41.     width:250px;
  42.     bordersolid 1px black;
  43.     margin0px 0px 0px 20px;
  44.     background-color:white;
  45.     filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=5, OffY=5, Color=#2557ad); 
  46.     display:none;
  47. }
  48. label
  49. {
  50.     font-size14px;
  51.     font-weightbold;
  52.     padding5px 5px 10px 15px;
  53. }
  54. input
  55. {
  56.     bordersolid 1px #000;
  57. }

2)定义弹出方法(js):

  1. var jsAreaShowTime = 1000;
  2. var JsAreas = new Object();
  3. function GetArea(id)
  4. {
  5.     return JsAreas[id] ? JsAreas[id] : (JsAreas[id] = new JsArea(id));
  6. }
  7. // A single popup window
  8. function JsArea(id)
  9. {
  10.     this.AreaId = id;
  11. }
  12. // Function to return the DIV Layer
  13. JsArea.prototype.ContentArea= function()
  14. {
  15.     var divArea = document.getElementById(this.AreaId);
  16.     if (divArea != null)
  17.     {
  18.         return divArea;
  19.     }
  20.     return null;
  21. }
  22. var activeAreaId = null;
  23. // Function to show the DIV Layer
  24. JsArea.prototype.popareaup = function(x, y)
  25. {
  26. if (activeAreaId != null)   
  27.     jsAreaClose(activeAreaId);
  28.     
  29.     var divLayer = this.ContentArea();
  30.     divLayer.style.position = 'absolute';
  31.     divLayer.style.display = 'block';
  32.     divLayer.style.left = x;
  33.     divLayer.style.top = y;
  34.     divLayer.οnmοuseοver= JsAreaMouseOver;
  35.     divLayer.onmouseout = jsAreaMouseOut;
  36.     activeAreaId = this.AreaId;
  37.     
  38.     return false;
  39. }
  40. // Function to hide the DIV Layer
  41. JsArea.prototype.hide = function()
  42. {
  43.     var divLayer = this.ContentArea();
  44.     if (divLayer != null)
  45.         divLayer.style.display = 'none';
  46.         
  47.     return false;
  48. }
  49. // Function to be called
  50. // by Web forms to show the Popup Window
  51. function PopupArea(e, areaId)
  52. {
  53.     if (e.pageX || e.pageY)
  54.     {
  55.         posx = e.pageX;
  56.         posy = e.pageY;
  57.     }
  58.     else 
  59.     if (e.clientX || e.clientY)
  60.     {
  61.         posx = e.clientX + document.body.scrollLeft;
  62.         posy = e.clientY + document.body.scrollTop;
  63.     }
  64.     var area = GetArea(areaId);
  65.     area.popareaup(posx, posy);
  66. }
  67. // Function to hide the DIV Layer
  68. function jsAreaClose(areaId)
  69. {
  70.     GetArea(areaId).hide();
  71.     activeAreaId = divHangTimer = null
  72. }
  73. var divHangTimer = null;
  74. // Function to keep the Div Layer
  75. // showing for a "period" of time
  76. // after that period, if the mouse
  77. // has been outside the DIV Layer, 
  78. // it will be hidden automatically
  79. function KeepArea(areaId)
  80. {
  81.     if (areaId == activeAreaId && divHangTimer != null)
  82.     {
  83.         clearTimeout(divHangTimer);
  84.         divHangTimer = null;
  85.     }
  86. }
  87. // Function to release the DIV Layer
  88. function RelArea(areaId)
  89. {
  90.     if (areaId == activeAreaId && divHangTimer == null)
  91.         divHangTimer = setTimeout('jsAreaClose(/'' + areaId + '/')', jsAreaShowTime);
  92. }
  93. // Function fired when mouse is over the 
  94. // DIV Layer, used to keep the layer showing
  95. function JsAreaMouseOver(e)
  96. {
  97.     if (!e) 
  98.         var e = window.event;
  99.     var targ = e.target ? e.target : e.srcElement;
  100.     KeepArea(activeAreaId);
  101. }
  102. // Function that fires when mouse is out of
  103. // the scope of the DIV Layer
  104. function jsAreaMouseOut(e)
  105. {
  106.     if (!e) 
  107.         var e = window.event;
  108.     var targ = e.relatedTarget ? e.relatedTarget : e.toElement;
  109.     var activeAreaView = document.getElementById(activeAreaId);
  110.     if (activeAreaView != null && !jsAreaContains(activeAreaView, targ))
  111.         RelArea(activeAreaId);
  112. }
  113. function jsAreaContains(parent, child)
  114. {
  115.     while(child)
  116.         if (parent == child) return true;
  117.         else 
  118.             child = child.parentNode;
  119.         
  120.         return false;
  121. }

 

3)调用实现:将 css,js脚本 生成相应文件 名称分别为 CSS.css 和js.js

  1. <!--head 区域内-->
  2. <LINK href="css.css" type="text/css" rel="stylesheet">
  3. <script src="js.js" type="text/javascript"></script>
  4. <script language="javascript">
  5.             <!--
  6.                                 function ProtectBox(e) 
  7.     {
  8.        return false;
  9.     }
  10.             //-->
  11.             </script>
  12. <!--body区域内->
  13. <!--注:最好用客户端控件-->
  14. <input id="Button1" type="button" value="button" OnKeyDown="return ProtectBox(event);" OnClick="PopupArea(event, 'divCountry')"/>
  15. <!--弹出的div -->
  16. <div class="popupWindow" id="divCountry">
  17.                 <table cellSpacing="0" cellPadding="0" width="250px" bgColor="#2557ad" border="0">
  18.                     <tr>
  19.                         <td align="right"><span style="CURSOR: hand" onclick="jsAreaClose('divCountry')"><img alt="Hide Popup" src="../../FormStyle/images/close.gif" border="0"></span></td>
  20.                     </tr>
  21.                     <tr>
  22.                         <td>
  23.                             <asp:Label ID="Label1" runat="server" valign="top" Text="驳回原因"></asp:Label>
  24.                             
  25.                             
  26.                         </td>
  27.                     </tr>
  28.                     <tr>
  29.                         <td>
  30.                             
  31.                             <asp:TextBox ID="TextBox2" runat="server" Height="83px" TextMode="MultiLine"></asp:TextBox>
  32.                             
  33.                             
  34.                         </td>
  35.                     </tr>
  36.                     <tr>
  37.                         <td>
  38.                           
  39.                             <asp:Button ID="Button3" runat="server" CssClass="btn1"  Text="确定" OnClick="Button1_Click" />
  40.                             
  41.                         </td>
  42.                     </tr>
  43.                 </table>
  44.             </div>
2、服务器端:即CS代码

 

  1. override protected void OnInit(EventArgs e)
  2.         {
  3.            base.OnInit(e);
  4.         }

填入这段代码即可

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值