Click here to Skip to main content
Click here to Skip to main content

Passing data and objects between parent and child web forms

, 23 Aug 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Passing data and objects between parent and child web forms.

Introduction

This article will show how to send simple data and data objects such as DataTable and DataSet between two web forms. This can be used to perform a certain kind of search or calculations in a separate form and then post the results back to the parent from.

Passing simple data between parent and child forms

Parent page:

<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">

function GetValueFromChild(myVal)
{
    document.getElementById('mytxt').value = myVal;
}

function OpenWindow()
{
    window.open("chiled.aspx", "newwin", 
       "height=250,width=250,toolbar=no,scrollbars="+
       scroll+",menubar=no");
}

</script>

</head>

<body>
<form id="form1" runat="server">
<div>
<input id="mytxt" type="text" />
<input type ="button" value="chiled window" onclick="OpenWindow()" />
</div>
</form>
</body>
</html>

Child page:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function SendValueToParent()
{
    var myVal = document.getElementById('mytxt').value;
    //window.opener.GetValueFromChild(myVal);
    //window.close();
    //return false;
    window.opener.form1.mytxt.value = myVal;
    window.close();
    return false;
    //window.opener.form1.submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="mytxt" type="text" />
<input id="btn1" type="button" 
   value="Send Value to Parent" 
   onclick="javascript:return SendValueToParent();" />
</div>
</form>
</body>
</html>

Passing a DataTable object from child to parent web form

Parent page:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script type ="text/javascript">
function openchiled()
{
    window.open("DataTable.aspx", "newwin", 
       "height=250,width=250,toolbar=no,scrollbars="+
       scroll+",menubar=no");
    return false;
}
</script>
<title>Parent Data Table</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<a href="#" onclick="openchiled()" >Open Child dataTable</a>
</div>
</form>
</body>
</html>

Child page:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" Text="Send DataTable" 
   runat="server" OnClick="LinkButton1_Click"></asp:LinkButton>
</div>
</form>
</body>
</html>

Child C# code:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.Add("FName", typeof(System.String));
    dt.Columns.Add("LName", typeof(System.String));
    System.Data.DataRow DR = dt.NewRow();
    DR["FName"] = "First Name";
    DR["LName"] = "Last Name";
    dt.Rows.Add(DR);
    dt.AcceptChanges();
    Session["DT"] = dt;
    Response.Write("<script type =text/javascript> " + 
       "window.opener.form1.submit(); window.close(); </script>");
}

I hope that you will find this useful. Development to me is a pleasure more than a job.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mohammad Al Hoss
Software Developer CME Offshore
Lebanon Lebanon
No Biography provided

Comments and Discussions

 
Generalsolution for Firefox Pinmemberfotr30-Nov-10 19:39 

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
Web01 | 2.8.141216.1 | Last Updated 23 Aug 2008
Article Copyright 2008 by Mohammad Al Hoss
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid