父窗口aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="UI_Default2" %>
<!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 runat="server">
<title>无标题页</title>
</head>
<script language=javascript type="text/jscript">
function popup() {
window.showModalDialog("Default3.aspx", "", "dialogWidth:40;dialogHeight:30;dialogLeft:50px;dialogTop:100px;help:no");
}
</script>
<body>
<form id="form1" runat="server">
<div>
<h1>学生管理</h1>
<br>
<asp:HiddenField ID="hfClassid" runat="server"/>
<table>
<tr>
<td style="width: 36px">
<asp:Label ID="Label1" runat="server" Text="班级"></asp:Label>
</td>
<td style="width: 68px">
<asp:Button ID="btnList" runat="server" Text="一览" Width="57px" OnClientClick="javascript:popup();"/>
</td>
<td style="width: 209px">
<asp:TextBox ID="txtClassName" runat="server" ReadOnly=true BackColor="Silver"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSearch" runat="server" Text="检索" OnClick="btnSearch_Click" />
</td>
</tr>
</table>
</div>
<asp:Panel ID="Panel1" runat="server" Height="261px" ScrollBars="Vertical" Width="404px" BorderStyle="Inset">
<asp:GridView ID="GridView1" runat="server" Width="405px" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</asp:Panel>
</form>
</body>
</html>
父窗口cs:
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;
using System.Data.SqlClient;
public partial class UI_Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 判断session是否存有子页面的值
if (Session["classname"] != null)
{
// 将session中的值设置到父页面上
this.txtClassName.Text = Session["classname"].ToString();
this.hfClassid.Value = Session["classid"].ToString();
}
// 清空session
Session["classid"] = null;
Session["classname"] = null;
}
/// <summary>
/// 检索按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSearch_Click(object sender, EventArgs e)
{
// 班级id
int id = Convert.ToInt32(this.hfClassid.Value.ToString().Trim());
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select stu.sid,stu.name,stu.age,stu.sex,class.classname from class, stu where class.cid = stu.cid and stu.cid = " + id, con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
con.Open();
DataSet ds = new DataSet();
ad.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
}
子窗口aspx:
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="Default3.aspx.cs" Inherits="UI_Default3" %>
<!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 runat="server">
<title>无标题页</title>
<base target="_self" />
</head>
<script language=javascript type="text/jscript">
function shutdown() {
window.close();
}
</script>
<body>
<form id="form1" runat="server">
<div>
<h1>班级一览</h1>
<br>
<asp:Panel ID="Panel1" runat="server" Height="200px" Width="446px" BorderStyle="Inset" ScrollBars="Vertical">
<asp:GridView ID="GridView1" runat="server" Width="444px" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" Visible="false" />
</Columns>
</asp:GridView>
</asp:Panel>
<table>
<tr>
<td style="width: 349px"></td>
<td>
<asp:Button ID="btnEnter" runat="server" Text="确定" OnClick="btnEnter_Click"/>
</td>
<td>
<asp:Button ID="btnCancal" runat="server" Text="取消" OnClientClick="javascript:shutdown();"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
子窗口cs:
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;
using System.Data.SqlClient;
public partial class UI_Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=MyDb;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from class", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
con.Open();
DataSet ds = new DataSet();
ad.Fill(ds);
ds.Tables[0].Columns[0].ColumnName = "班级ID";
ds.Tables[0].Columns[1].ColumnName = "班级";
ds.Tables[0].Columns[2].ColumnName = "班级位置";
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
con.Close();
}
/// <summary>
/// 选择班级row
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// 不是header
if (e.Row.RowIndex != -1)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
/// <summary>
/// 确定按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnEnter_Click(object sender, EventArgs e)
{
string classid = this.GridView1.SelectedRow.Cells[1].Text.Trim();
string classname = this.GridView1.SelectedRow.Cells[2].Text.Trim();
// 将id和班级保存在会话状态中
Session["classid"] = classid;
Session["classname"] = classname;
// 关闭窗口
Response.Write("<script>window.opener=null;window.close();</script>");
}
}