ASP.NET父子視窗的應用,利用子視窗的Gridview選取一個值,傳回父視窗

有2個頁面,分別為A頁面(父)、B頁面(子)。
當在A頁面按下一個按鈕,就會開啟B頁面。
B頁面裡有一個Gridview,按Gridview的Select,將選取的值帶回A頁面。

有2個頁面,分別為A頁面(父)、B頁面(子)。
當在A頁面按下一個按鈕,就會開啟B頁面。
B頁面裡有一個Gridview,按Gridview的Select,將選取的值帶回A頁面。

asp.net(C#)

A.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="A.aspx.cs" Inherits="A" %>

<!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>A</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="選取" OnClientClick="window.open('B.aspx'); return false;" />
        </div>
    </form>
</body>
</html>

A.aspx.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;


public partial class A : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

}

B.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="B.aspx.cs" Inherits="B" %>

<!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>B</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
                <Columns>
                    <asp:CommandField ShowSelectButton="True" SelectText="Select"></asp:CommandField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

B.aspx.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;


public partial class B : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.GridView1.DataSource = new string[] { "Dotblogs", "F6 Team", "puma" };
        this.GridView1.DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "Select")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = this.GridView1.Rows[index];
            //傳值給父頁面
            this.Page.Controls.Add(new LiteralControl(string.Format("<script>opener.document.form1.TextBox1.value='{0}'</script>", row.Cells[1].Text)));

            //關閉此視窗
            this.Page.Controls.Add(new LiteralControl("<script>window.close();</script>"));
        }

    }

}

執行結果:

參考網址:

http://www.blueshop.com.tw/board/show.asp?subcde=BRD20080426110908CDR&fumcde=FUM20041006161839LRJ