Click here to Skip to main content
15,894,405 members
Articles / Web Development / HTML

Passing data and objects between parent and child web forms

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
23 Aug 2008CPOL 39.1K   446   10   1
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:

HTML
<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
<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
<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
<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:

C#
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)


Written By
Software Developer CME Offshore
Lebanon Lebanon
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalsolution for Firefox Pin
fotr30-Nov-10 18:39
fotr30-Nov-10 18:39 

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

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