First of all I apologize since I have not posted any articles lately. I was extremely busy with my University Studies and interviews (This is my last semester :) ). Anyway I have come back with another good article. Many people have asked me how can I pass value from child page to a parent page. In this article I will show you that how you can make some selections in the child page and pass those selections to the parent page.

 

Introduction:

First of all I apologize since I have not posted any articles lately. I was extremely busy with my University Studies and interviews (This is my last semester :) ). Anyway I have come back with another good article. Many people have asked me how can I pass value from child page to a parent page. In this article I will show you that 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 consists 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");
}


</script>

 

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 contains a GridView control. GridView control will also have checkboxes so you can select various rows. Once you click the Button you 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 find which rows are selected and finally put the Items in the DataTable object. In the example above I am using 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 later you change the position of the columns then you don't need to change anything in the code.

Once, you got 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! Don't forget to download the sample files.

I hope you liked the article, happy coding!