asp.net,网页层与div的数据交互

本文介绍了一个使用DIV实现的登录层与半透明遮罩层的案例,详细讲解了如何通过JavaScript来控制这些层的显示与隐藏,并实现了屏幕适配。此外还介绍了如何获取登录层中复选框的选择值。

 这几天忙着一个模块的开发,真是差点没把我弄疯了!现在终于叹口气了,解决了几个大问题!

在我做的那个模块里涉及到div的相对位置的设置,网页层与div显示层之间数据的传递,之前我对div根本就不了解,经过这么一折腾啊,不得不明白了!好了,言归正传!

这是我的试验例子,有点乱,多多包含:

代码部分:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <script type="text/javascript" language="javascript"
  4.         function ShowNo()                        //隐藏两个层 
  5.         { 
  6.             document.getElementById("doing").style.display="none"
  7.             document.getElementById("divLogin").style.display="none"
  8.             getCheckValue();
  9.         } 
  10.         function $(id)        
  11.         { 
  12.             return (document.getElementById) ? document.getElementById(id) : document.all[id] ; 
  13.         } 
  14.         function showFloat(leftlocal)                    //根据屏幕的大小显示两个层 
  15.         { 
  16.             var range = getRange(); 
  17.         
  18.             $('doing').style.width = range.width + "px"
  19.             $('doing').style.height = range.height + "px"
  20.             $('doing').style.display = "block"
  21.             document.getElementById("divLogin").style.display=""
  22.             
  23.         } 
  24.         function getRange()                      //得到屏幕的大小 
  25.         { 
  26.               var top    = document.body.scrollTop; 
  27.               var left    = document.body.scrollLeft; 
  28.               var height  = document.body.clientHeight; 
  29.               var width  = document.body.clientWidth; 
  30.               if (top==0 && left==0 && height==0 && width==0) 
  31.               { 
  32.                 top    = document.documentElement.scrollTop; 
  33.                 left    = document.documentElement.scrollLeft; 
  34.                 height  = document.documentElement.clientHeight; 
  35.                 width  = document.documentElement.clientWidth; 
  36.               } 
  37.               return  {top:top  ,left:left ,height:height ,width:width } ; 
  38.         }
  39.         
  40.        function getCheckValue()
  41.        {
  42.             var theBox=document.all("divLogin")
  43.             var str=""
  44.             elm=theBox.getElementsByTagName('Input')
  45.             for(i=0;i<elm.length;i++)
  46.             {
  47.                 if(elm[i].type=="checkbox")
  48.                 {
  49.              
  50.                     if (elm[i].checked==true)
  51.                     {
  52.                         //获得客户端checkbox的值
  53.                         str+=elm[i].nextSibling.innerText+",";
  54.                     }
  55.                 }
  56.             }
  57.           
  58.             document.getElementById("txtValue").value=str;
  59.              document.getElementById("Hidden1").value=str;
  60.               //document.write(document.getElementById("txtValue").value);
  61.        }
  62.     
  63. </script>
  64. <html xmlns="http://www.w3.org/1999/xhtml">
  65. <head runat="server">
  66.     <title>Div显示层</title>
  67. </head>
  68. <body>
  69.     <form id="form1" runat="server">
  70.   
  71.         <br />
  72.         <br />
  73.         <br />
  74.         <p>
  75.             <input id="Hidden1" type="hidden" runat="server" />
  76.         </p>
  77.         <asp:TextBox ID="txtBox" runat="server"></asp:TextBox>
  78.         <!--<a href="javascript:void(0)" onclick="">登陆 </a>//登陆链接-->
  79.         <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
  80.         <input type="button" value="登陆" onclick="showFloat()" />
  81.         <input id="txtValue" type="text" onfocus="showFloat()" />
  82.        
  83.            <!--加一个半透明层-->
  84.       <div id="doing" style="filter: alpha(opacity=30); -moz-opacity: 0.3; opacity: 0.3;
  85.             background-color: Gray; width: 100%; height: 100%; z-index: 1000; position: absolute;
  86.             left: 0; top: 0; display: none; overflow: hidden;">
  87.         </div>
  88.         <!--加一个登录层-->
  89.         <div id="divLogin" style="background: #fff; padding: 10px 0 0 200px; width: 560px;
  90.             z-index: 1001; position: relative; display: none; left: expression(document.getElementById('<%=this.txtBox.ClientID %>').offsetLeft+200+ 'px');
  91.             ); top: expression(document.getElementById('<%=this.txtBox.ClientID%>').offsetTop+150+ 'px');
  92.             margin: -200px 0 0 -400px; border-right: silver 1px solid; border-top: silver 1px solid;
  93.             border-left: silver 1px solid; border-bottom: silver 1px solid;">
  94.             <div style="padding: 3px 15px 3px 15px; text-align: left; vertical-align: middle;">
  95.                 <div>
  96.                     <h4>
  97.                         请选择你要发送的对象</h4>
  98.                     用户:
  99.                     <asp:TextBox ID="TxtUserName" runat="server"> </asp:TextBox>
  100.                 </div>
  101.                 <div style="border-top-width: 1px; border-left-width: 1px; border-left-color: silver;
  102.                     border-bottom-width: 1px; border-bottom-color: silver; border-top-color: silver;
  103.                     border-right-width: 1px; border-right-color: silver;">
  104.                     密码:
  105.                     <asp:TextBox ID="TxtUserPwd" runat="server" TextMode="Password"> </asp:TextBox>
  106.                 </div>
  107.                 <br />
  108.                 <div>
  109.                        
  110.                     <asp:Button ID="BttLogin" runat="server" Text=" 登 陆 " /> 
  111.                     <input id="BttCancel" type="button" value=" 取 消 " onclick="ShowNo()" />
  112.                 </div>
  113.                 <div style="width: 100%">
  114.                     <table width="100%" border="1">
  115.                         <tr>
  116.                             <td align="right">
  117.                                 </td>
  118.                         </tr>
  119.                         <tr>
  120.                             <td>
  121.                                 <asp:Panel ID="Panel1" runat="server" Direction="LeftToRight" ScrollBars="Auto" Width="100%"
  122.                                     Height="90px" HorizontalAlign="Center">
  123.                                     <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="8" Width="95%">
  124.                                         <asp:ListItem Value="0">文件</asp:ListItem>
  125.                                         <asp:ListItem Value="2">数码</asp:ListItem>
  126.                                         <asp:ListItem Value="1">书本</asp:ListItem>
  127.                                         <asp:ListItem Value="3">电器</asp:ListItem>
  128.                                         <asp:ListItem Value="4">娱乐</asp:ListItem>
  129.                                         <asp:ListItem Value="5">军事</asp:ListItem>
  130.                                         <asp:ListItem Value="36">地球</asp:ListItem>
  131.                                         <asp:ListItem Value="89">环境</asp:ListItem>
  132.                                         <asp:ListItem Value="3">经济</asp:ListItem>
  133.                                         <asp:ListItem Value="3">政治</asp:ListItem>
  134.                                         <asp:ListItem Value="3">电器</asp:ListItem>
  135.                                         <asp:ListItem Value="3">娱乐</asp:ListItem>
  136.                                         <asp:ListItem Value="3">军事</asp:ListItem>
  137.                                         <asp:ListItem Value="3">地球</asp:ListItem>
  138.                                         <asp:ListItem Value="3">环境</asp:ListItem>
  139.                                         <asp:ListItem Value="3">经济</asp:ListItem>
  140.                                     </asp:CheckBoxList>
  141.                                 </asp:Panel>
  142.                                 
  143.                             </td>
  144.                         </tr>
  145.                     </table>
  146.                 </div>
  147.             </div>
  148.         </div>
  149.          
  150.     </form>
  151. </body>
  152. </html>

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值