父窗口:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="button" onclick="openNewForm()" />
<asp:Panel ID="Panel1" runat="server" Width="500px" style="border:solid 1px red">
</asp:Panel>
</div>
</form>
<script type="text/javascript">
function openNewForm()...{
window.open("default8.aspx");
//假设打开default8.aspx这个页面
}
</script>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="button" onclick="window.close();" />
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
<script type="text/javascript">
function initCheckBoxChecked()...{
var oContainer=document.getElementById("<%=GridView1.ClientID %>");
var chks=oContainer.getElementsByTagName("input");
for(var i=0;i!=chks.length;++i)
if(chks[i].type="checkbox")
chks[i].onclick=onCheckBoxChecked;
}
// function onCheckBoxChecked(srcElm){
// if(srcElm.checked && opener!=null){
// var prtPanel=opener.document.getElementById("Panel1");
// var row=srcElm.parentNode.parentNode;
// var cells=row.getElementsByTagName("td");
// var isIe=window.navigator.appName.indexOf("Netscape") == -1?true:false;
// prtPanel.innerHTML+="您刚才选择的是 - ID:";
// prtPanel.innerHTML+=isIe?cells[1].innerText:cells[1].textContent;
// prtPanel.innerHTML+=" Name: ";
// prtPanel.innerHTML+=isIe?cells[2].innerText:cells[2].textContent;
// prtPanel.innerHTML+="<br />";
// }
// }

function onCheckBoxChecked(srcElm)...{
var prtPanel=opener.document.getElementById("Panel1");
var isIe=window.navigator.appName.indexOf("Netscape") == -1?true:false;
var rows=document.getElementById("<%=GridView1.ClientID %>").getElementsByTagName("tr");
var sHtml="";
var cells;
var sId;
var sName;
var sText;
for(var i=1;i!=rows.length;++i)...{
var oChks=rows[i].getElementsByTagName("input");
if(oChks[0].checked)...{
cells=rows[i].getElementsByTagName("td");
sId=isIe?cells[1].innerText:cells[1].textContent;
sName=isIe?cells[2].innerText:cells[2].textContent;
sText="<span id=""+sId+"">您刚才选择的是 - ID:"+sId+", Name: "+sName+"</span><br />";
sHtml+=sText;
}
}
prtPanel.innerHTML=sHtml;
}
</script>
</body>
</html>
子窗口C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default8 : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
GridView1.DataSource = GenerateTable();
GridView1.DataBind();
}
private DataTable GenerateTable()
...{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
//dt.Columns.Add("Price", typeof(decimal));
DataRow row;
Random rnd = new Random();
for (int i = 1; i != 31; ++i)
...{
row = dt.NewRow();
row[0] = i;
row[1] = "Product_" + i;
//row[2] = Math.Round(rnd.Next(20, 100) / 1.48D, 2);
dt.Rows.Add(row);
}
return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
...{
CheckBox chk = e.Row.FindControl("chk") as CheckBox;
if (chk != null)
chk.Attributes.Add("onclick", "onCheckBoxChecked(this)");
}
}
本文介绍了一个ASP.NET应用程序中父窗口与子窗口之间的数据交互案例。通过使用JavaScript和C#,实现了从子窗口向父窗口传递数据的功能,并展示了如何在子窗口关闭前将选中的数据更新到父窗口。
386

被折叠的 条评论
为什么被折叠?



