Click here to Skip to main content
Licence CPOL
First Posted 23 Aug 2008
Views 13,827
Downloads 129
Bookmarked 10 times

Passing data and objects between parent and child web forms

By | 23 Aug 2008 | Article
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)

About the Author

Mohammad Al Hoss

Software Developer
CME Offshore
Lebanon Lebanon

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalsolution for Firefox Pinmemberfotr18:39 30 Nov '10  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 23 Aug 2008
Article Copyright 2008 by Mohammad Al Hoss
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid