在ASP.NET中,很多时候需要用到弹出窗口,具体何处使用要看个人需求,废话不多说,直接上示例。
上图为单选,即先弹出一个窗口,选择信息后关闭弹出窗口,然后设置原页面上所需弹窗数据控件内容为所选内容。
上图为多选,即弹出一个窗口后选择所需信息后,不关闭弹出窗口原页面中对应需要获取弹出窗口信息的控件的内容也随之变化。
具体代码如下所示:
原页面:
<SCRIPT language=javascript>
/*其中toid为隐藏控件的ID,保存返回的ID,toname为textbox,显示范围的名称。不管是单选还是多选,原页面只有JAVASCRIPT方法*/
function group_select(toid,toname)
{
var url= "../../../module/group_select/index.aspx?ToId="+toid+"&ToName="+toname;
var mwidth = "400";
var mheight = "330";
var loc_x,loc_y;
if(window.navigator.appName.toLowerCase().indexOf("netscape") > -1)
{
loc_x = parseInt((document.body.clientWidth - mwidth)/2) + 200;
loc_y = parseInt((document.body.clientHeight - mheight)/2);
window.open(url,"group_select","left=" + loc_x + "px,top=" + loc_y + "px,width=" + mwidth + "px,height=" + mheight + "px,resizable=no,scrollbars=yes,status=0");
}
else
{
loc_x=document.body.scrollLeft+event.clientX-event.offsetX-100;
loc_y=document.body.scrollTop+event.clientY-event.offsetY+170;
window.showModalDialog(url,self,"edge:raised;scroll:1;status:0;help:0;resizable:1;dialogWidth:"+mwidth+"px;dialogHeight:"+mheight+"px;dialogTop:"+loc_y+"px;dialogLeft:"+loc_x+"px");
}
}
</SCRIPT>
弹窗页面单选主要代码(单选):
<html>
<head>
<title>设备</title>
<script language=javascript>
var parent_window;
if(window.navigator.appName.toLowerCase().indexOf("netscape") > -1)
parent_window = window.parent.opener;
else
parent_window = parent.dialogArguments;
function click_place(PartID,PartName)
{
parent_window.document.getElementById("<% = PartID %>").value = PartID;
parent_window.document.getElementById("<% = PartName %>").value = PartName;
returnValue="OK";
window.close();/* 关闭窗口 */
}
</script>
</head>
<body class="bodycolor" topmargin="1" leftmargin="0">
<form id="Form1" method="post" runat="server">
<table border="0" cellspacing="0" width="100%" class="small" cellpadding="3" bordercolorlight="#000000" bordercolordark="#ffffff">
<tr class="TableHeader">
<td colspan="2" align="center">
<b><% =FaultType%></b>
</td>
</tr>
<asp:Repeater ID="rptPlace" runat="server">
<ItemTemplate>
<tr class="TableLine1">
<td align="left">
<a href="javascript:click_place('<%#Eval("PartID")%>','<%#Eval("PartName")%>');"><%#DataBinder.Eval(Container.DataItem, "PartName")%></a>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="TableLine2">
<td align="left">
<a href="javascript:click_place('<%#Eval("PartID")%>','<%#Eval("PartName")%>');"><%#DataBinder.Eval(Container.DataItem, "PartName")%></a>
</td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>
弹出窗口(多选)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="TollFault.module.group_select.index" %>
<HTML>
<HEAD>
<title>选择角色</title>
<style> .menulines { CURSOR: hand } </style>
<script Language="JavaScript">
var privlist=new Array();
<%=strPrivList%>
var parent_window;
if(window.navigator.appName.toLowerCase().indexOf("netscape") > -1)
parent_window = window.opener;
else
parent_window = window.dialogArguments;
function click_priv(priv_id)
{
TO_VAL=parent_window.document.getElementById("<% =ViewState["ToId"] %>").value;
TO_NAME=parent_window.document.getElementById("<% =ViewState["ToName"] %>").value;
targetelement=document.getElementById("PRIV_"+priv_id);
priv_name=targetelement.getAttribute("name");
if(TO_VAL.indexOf(","+priv_id+",")>0 || TO_VAL.indexOf(priv_id+",")==0)
{
if(TO_VAL.indexOf(priv_id+",")==0)
parent_window.document.getElementById("<% =ViewState["ToId"] %>").value=parent_window.document.getElementById("<% =ViewState["ToId"] %>").value.replace(priv_id+",","");
if(TO_VAL.indexOf(","+priv_id+",")>0)
parent_window.document.getElementById("<% =ViewState["ToId"] %>").value=parent_window.document.getElementById("<% =ViewState["ToId"] %>").value.replace(","+priv_id+",",",");
if(TO_NAME.indexOf(priv_name+",")==0)
parent_window.document.getElementById("<% =ViewState["ToName"] %>").value=parent_window.document.getElementById("<% =ViewState["ToName"] %>").value.replace(priv_name+",","");
if(TO_NAME.indexOf(","+priv_name+",")>0)
parent_window.document.getElementById("<% =ViewState["ToName"] %>").value=parent_window.document.getElementById("<% =ViewState["ToName"] %>").value.replace(","+priv_name+",",",");
borderize_off(targetelement);
}
else
{
parent_window.document.getElementById("<% =ViewState["ToId"] %>").value+=priv_id+",";
parent_window.document.getElementById("<% =ViewState["ToName"] %>").value+=priv_name+",";
borderize_on(targetelement);
}
}
function borderize_on(targetelement)
{
color="#003FBF";
targetelement.style.borderColor="black";
targetelement.style.backgroundColor=color;
targetelement.style.color="white";
targetelement.style.fontWeight="bold";
}
function borderize_off(targetelement)
{
targetelement.style.backgroundColor="";
targetelement.style.borderColor="";
targetelement.style.color="";
targetelement.style.fontWeight="";
}
function begin_set()
{
TO_VAL=parent_window.document.getElementById("<% =ViewState["ToId"] %>").value;
if(TO_VAL=="ALL_PRIV")
{
parent_window.document.getElementById("<% =ViewState["ToId"] %>").value="";
parent_window.document.getElementById("<% =ViewState["ToName"] %>").value="";
}
for (step_i=0; step_i<privlist.length; step_i++)
{
priv_id=privlist[step_i];
if(TO_VAL.indexOf(","+priv_id+",")>0 || TO_VAL.indexOf(priv_id+",")==0)
borderize_on(document.getElementById("PRIV_"+privlist[step_i]));
}
}
function add_all()
{
TO_VAL=parent_window.document.getElementById("<% =ViewState["ToId"] %>").value;
for (step_i=0; step_i<privlist.length; step_i++)
{
priv_id=privlist[step_i];
priv_name=document.getElementById("PRIV_"+privlist[step_i]).getAttribute("name");
if(TO_VAL.indexOf(","+priv_id+",")<=0 && TO_VAL.indexOf(priv_id+",")!=0)
{
parent_window.document.getElementById("<% =ViewState["ToId"] %>").value+=priv_id+",";
parent_window.document.getElementById("<% =ViewState["ToName"] %>").value+=priv_name+",";
borderize_on(document.getElementById("PRIV_"+privlist[step_i]));
}
}
}
function add_all_priv()
{
parent_window.document.getElementById("<% =ViewState["ToId"] %>").value="ALL_PRIV";
parent_window.document.getElementById("<% =ViewState["ToName"] %>").value="全体分组";
parent.close();
}
function del_all()
{
for (step_i=0; step_i<privlist.length; step_i++)
{
TO_VAL=parent_window.document.getElementById("<% =ViewState["ToId"] %>").value;
TO_NAME=parent_window.document.getElementById("<% =ViewState["ToName"] %>").value;
priv_id=privlist[step_i];
priv_name=document.getElementById("PRIV_"+privlist[step_i]).getAttribute("name");
if(TO_VAL.indexOf(priv_id+",")==0)
parent_window.document.getElementById("<% =ViewState["ToId"] %>").value=parent_window.document.getElementById("<% =ViewState["ToId"] %>").value.replace(priv_id+",","");
if(TO_VAL.indexOf(","+priv_id+",")>0)
parent_window.document.getElementById("<% =ViewState["ToId"] %>").value=parent_window.document.getElementById("<% =ViewState["ToId"] %>").value.replace(","+priv_id+",",",");
if(TO_NAME.indexOf(priv_name+",")==0)
parent_window.document.getElementById("<% =ViewState["ToName"] %>").value=parent_window.document.getElementById("<% =ViewState["ToName"] %>").value.replace(priv_name+",","");
if(TO_NAME.indexOf(","+priv_name+",")>0)
parent_window.document.getElementById("<% =ViewState["ToName"] %>").value=parent_window.document.getElementById("<% =ViewState["ToName"] %>").value.replace(","+priv_name+",",",");
borderize_off(document.getElementById("PRIV_"+privlist[step_i]));
}
}
</script>
</HEAD>
<body topmargin="1" leftmargin="0" class="bodycolor" οnlοad="begin_set();">
<form id="Form1" method="post" runat="server">
<table border="1" cellspacing="0" width="95%" class="small" cellpadding="3" bordercolorlight="#000000"
bordercolordark="#ffffff" align="center">
<tr class="TableContent">
<td class="menulines" οnclick="javascript:add_all();" align="center">全部添加</td>
</tr>
<tr class="TableContent">
<td class="menulines" οnclick="javascript:del_all();" align="center">全部删除</td>
</tr>
<asp:Literal id="litPriv" runat="server"></asp:Literal>
</table>
</form>
</body>
</HTML>
后台代码
private void LoadData()
{
string lit = "";
Utilities.DBConnect db = new Utilities.DBConnect();
DataTable dt = db.ExecuteDataTable("Select * from TMobileGroup order by GID");
if (dt == null) { return; }
for (int i = 0; i < dt.Rows.Count; i++)
{
string GID = dt.Rows[i]["GID"].ToString();
string GName = dt.Rows[i]["GName"].ToString();
strPrivList += "privlist[" + i.ToString() + "]=" + GID + ";\r\n";
lit += "<tr class='TableControl'>\r\n";
lit += "<td class='menulines' id='PRIV_" + GID + "' name='" + GName + "' οnclick='javascript:click_priv(" + GID + ")' align=left>" + GName + "</td>\r\n";
lit += "</tr>\r\n";
}
litPriv.Text = lit;
}
这是借鉴思道OA的弹窗做出的。东西简单,不多说了。