本文 bilal haidar 将带领您如何使用div元素来创建弹出的窗体,这种弹出即可以包含简单的html元素也可以包含asp.net服务器控件,而且在实现过程中没有使用传统的window函数和showmodaldialog / showmodelessdialog函数(传统的我们使用 window.open,或者showmodaldialog 这样的函数来制作弹出窗口--天天注释)
最近我在用asp.net1.1技术来开发一个窗体,该窗体包含由三个控件组成的一个面板集合,这个面板用来显示系统信息.可以假想这些控件是一些简单的下拉框,当第一个下拉框选取后,第二个下拉框的值将显示被第一个过滤的结果,同样第三个下拉框将根据第二个下拉框的选择而进行改变显示。
窗体的这个技术通常被用来让终端客户那些不知道asp.net技术的人员获取更好的用户体验。
当决定使用这些控件的替代品使用时,您是否用过dropdownlist或者是具有弹出窗体功能的textbox控件?
好了,我们已经有了一个很好的解决方案:使用textbox控件并挂钩onclick事件来触发div弹出窗体,包括使用listbox控件来选择数据的值
一个不使用任何常规popup窗体或者过时的dropdownlist来完成这个功能
the html webform
我们已经建立了一个简单的webform,他包含了一些textbox,每一个textbox已经附加了onclick事件,用一段javascript代码来弹出窗体,代码如下:
<%@ page language="c#"
codebehind="parentpage.aspx.cs" autoeventwireup="false"
inherits="popupwithdiv.parentpage" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>parent page</title>
<link href="main.css" type="text/css" rel="stylesheet">
<script src="jspopup.js" type="text/javascript"></script>
<script language="javascript">
<!--
// prevent users from typing any text
// into the textbox
function protectbox(e)
{return false; }
//-->
</script>
</head>
<body>
<form id="form1" method="post" runat="server">
<!-- header section -->
<div id="header">
<p>popup window with div layer</p>
</div>
<!-- body section -->
<div id="content">
<table border="0" cellpadding="0" cellspacing="0">
<tr valign="top">
<td><label for="txtcountry">country :</label></td>
<td><asp:textbox
id="txtcountry" runat="server" onkeydown="return
protectbox(event);" onclick="popuparea(event, 'divcountry')"></asp:textbox></td>
<td width="50"></td>
<td><label for="txtcity">city :</label></td>
<td><asp:textbox
id="txtcity" runat="server" onkeydown="return
protectbox(event);" onclick="popuparea(event, 'divcity')"></asp:textbox></td>
</tr>
</table>
</div>
<%-- country --%>
<div class="popupwindow" id="divcountry">
<table cellspacing="0" cellpadding="0" width="100%" bgcolor="#2557ad" border="0">
<tr>
<td align="right"><span style="cursor: hand"
onclick="jsareaclose('divcountry')"><img alt="hide popup" src="close.gif"
border="0"></span></td>
</tr>
<tr>
<td>
<asp:listbox id="lstcountry" runat="server" autopostback="true" width="100%"
rows="10"></asp:listbox></td>
</tr>
</table>
</div>
<%-- city --%>
<div class="popupwindow" id="divcity">
<table
cellspacing="0" cellpadding="0" width="100%"
bgcolor="#2557ad" border="0">
<tr>
<td align="right"><span style="cursor: hand" onclick="jsareaclose('divcity')"><img alt="hide popup" src="close.gif" border="0"></span></td>
</tr>
<tr>
<td>
<asp:listbox id="lscity" runat="server" autopostback="true" width="100%" rows="10"></asp:listbox> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
代码中,用粗体标出的部分是popup窗体的主要属性,在鼠标单击时,将调用一端javascript:popuparea。
正如您所看到的,我们在页面底部添加了两个div元素,一个用于国家,一个用于城市,每一个都包含listbox控件,用户可以使用listbox选择上面的内容。
下图1现实了页面浏览的效果,他还演示了如何弹出div窗体
当单击textbox内部,windows将弹出窗体而不会引起页面数据回发现在该到填充其中数据的时候了
page code-behind
在页面后台,我们准备从一个xml文档加载list“国家”所需要的数据,同时显示国家的名称,下面列出了这个功能的代码:
listing 2: populate country listbox
// load data into country list box
if (!page.ispostback)
{
// load data from xml into a dataset
dataset ds = new dataset();
ds.readxml(server.mappath("countries.xml"));
this.lstcountry.datasource = ds.tables[0].defaultview;
this.lstcountry.datatextfield = "name";
this.lstcountry.databind();
}
在这一步骤中,当页面运行时,您可以选择国家,如下图
现在,当用户选择国家时,将触发listbox的选择事件,并通过该事件加载“城市”数据,该数据同样从xml文档加载
下面列出了事件代码
listing 3
private void lstcountry_selectedindexchanged(object sender, eventargs e)
{
// set the value in the textbox
this.txtcountry.text = this.lstcountry.selectedvalue;
// load and filter the lstcity
dataset ds = new dataset();
ds.readxml(server.mappath("cities.xml"));
dataview dv = ds.tables[0].defaultview;
dv.rowfilter = "country = '" + this.lstcountry.selectedvalue + "'";
// bind lstcity
this.lstcity.datasource = dv;
this.lstcity.datatextfield = "name";
this.lstcity.databind();
}
用户现在可以选择与国家相匹配的城市,如下
