Click here to Skip to main content
11,704,768 members (56,631 online)
Click here to Skip to main content

Passing Values from Child Page to Parent Page

, 8 Dec 2005 111.1K 285 41
Rate this:
Please Sign up or sign in to vote.
In this article, I will show you how you can make some selections in the child page and pass those selections to the parent page.

Introduction

Many people have asked me how I can pass a value from a child page to a parent page. In this article, I will show you how you can make some selections in the child page and pass those selections to the parent page.

Creating the Parent Page

Our parent page will consist of a button and a GridView control. The button will simply open a new window (child window). Let's see the code for the parent page:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["SelectedItems"] != null)
    {
        GridView1.DataSource = (DataTable)Session["SelectedItems"];
        GridView1.DataBind();
    }
}

And to open the child window, write this code in the HTML view. The function OpenWindow is called when the button is clicked.

<input type="button" value="Open a new window" 
             onclick="OpenWindow()" id="Button1" />
function OpenWindow()
{
  window.open("NewWindow.aspx","MyWindow","height=450,width=300");
}

Okay, the above code will open a child window. Now let's see how the child window looks like:

Child Window

Our child window will contain a GridView control. The GridView control will also have checkboxes so you can select various rows. Once you click the button, the child window will close and you will see the selected items in the parent window.

Here is the button click event of the child page:

protected void Button1_Click(object sender, EventArgs e)
{
    // Make a datatable which will hold the values
    DataTable myTable = new DataTable();
    myTable.Columns.Add("CategoryID");
    myTable.Columns.Add("CategoryName");
    DataRow myRow = null;

    foreach (GridViewRow row in gvChild.Rows)
    {
        bool result = ((CheckBox) row.FindControl("CheckBox1")).Checked;

        if (result)
        {
            myRow = myTable.NewRow();
            myRow["CategoryID"] = row.Cells[0].Text;
            myRow["CategoryName"] = row.Cells[1].Text;
            myTable.Rows.Add(myRow);
        }
    }
    Session["SelectedItems"] = myTable;
}

All I am doing is looping through the GridView and finding which rows are selected, and finally I put the items in the DataTable object. In the example above, I am using the row.Cells[0].Text property of the GridViewRow but you can easily use row.FindControl("Id of the control"). The good thing about the FindControl method is that if you later change the position of the columns, you don't need to change anything in the code.

Once, you get the selected items in the DataTable object, simply place them in the session variable. Now the only task left to do is to make a postback on the parent window and close the child window on the button click.

Here is the code that will do the trick (this code is for the child window):

<body onunload="PassValues()">
<script language="javascript" type="text/javascript">

function PassValues()
{
    window.opener.document.forms(0).submit();
    self.close();
}
</script>

Yup that's it!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

azamsharp
Web Developer
United States United States
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.

HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.

HighOnCoding.com www.HighOnCoding.com


My Blog:

Blog



Buy my iPhone app ABC Pop

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 1071805314-Apr-14 23:20
memberMember 1071805314-Apr-14 23:20 
QuestionI have almost the same problem like this..My page contains 2 iframes which loads 2 different files.. Pin
Member 960756017-Nov-12 7:10
memberMember 960756017-Nov-12 7:10 
GeneralMy vote of 3 Pin
prasad_vjp19-Jan-12 2:29
memberprasad_vjp19-Jan-12 2:29 
Generalthis code is not working in mozilla firefox 3.5 Pin
patelnilu25-Jan-11 10:47
memberpatelnilu25-Jan-11 10:47 
Questionhow about the code in vb? Pin
alyy04069-Dec-08 17:02
memberalyy04069-Dec-08 17:02 
QuestionHow to manage session values when closing window ? Pin
Ramanichandran2-Jul-07 0:43
memberRamanichandran2-Jul-07 0:43 
GeneralIf DataTable is null then no postback in parent window Pin
BhaveshRadhik12-Mar-07 10:02
memberBhaveshRadhik12-Mar-07 10:02 
Questioni want to pass large data to new window with javascript in asp.net 2 Pin
shahmitan11-Jan-07 1:37
membershahmitan11-Jan-07 1:37 
hi all

i want to pass large data to new opened window from javascript

pls help me

i spend lots of time to findout how....

thanx in advance.....



Generalohh... u save my day@!!! Pin
maya_zakry27-Sep-06 16:07
membermaya_zakry27-Sep-06 16:07 
GeneralRe: ohh... u save my day@!!! Pin
azamsharp27-Sep-06 16:50
memberazamsharp27-Sep-06 16:50 
QuestionWrong Demo? Pin
HeYuBo24-Jun-06 17:44
memberHeYuBo24-Jun-06 17:44 
GeneralDo not want to Refresh (post) to parent page Pin
HemaRawat20-Apr-06 2:13
memberHemaRawat20-Apr-06 2:13 
GeneralPlease help Pin
Lavlaki8-Mar-06 9:04
memberLavlaki8-Mar-06 9:04 
GeneralRefreshing Parent Window Pin
Majid Shahabfar1-Jan-06 23:43
memberMajid Shahabfar1-Jan-06 23:43 
QuestionChild in another project Pin
RicLeitch15-Dec-05 3:32
memberRicLeitch15-Dec-05 3:32 
AnswerRe: Child in another project Pin
azamsharp19-Dec-05 19:48
memberazamsharp19-Dec-05 19:48 
GeneralRe: Child in another project Pin
RicLeitch21-Dec-05 8:26
memberRicLeitch21-Dec-05 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 8 Dec 2005
Article Copyright 2005 by azamsharp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid