ASP.NET 父視窗 呼叫 子視窗 並傳回值

今天要做一個開新視窗後,再透過新視窗選擇的值,在回傳給父視窗
為此達到效果,花了許多時間測試,因此把它筆記一下

這裡是使用ASP.NET C#

父視窗 A001.aspx



子視窗 B001.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("", row.Cells[1].Text)));
//抓值部分也可以改成 opener.document.getElementById('TextBox1').value  如下
//ContentPlaceHolder1_TextBox1 這個ID為動態產生的ID,建議可以透過父視窗 
//string sNewId = TextBox1.ClientID;

//        TextBox1.Text = sNewId;
            //關閉此視窗
            this.Page.Controls.Add(new LiteralControl(""));
        }
    }

} 

沒有留言:

張貼留言