Click here to Skip to main content
6,594,432 members and growing! (17,318 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Passing Data and objects between parent and child web forms

By Mohammad Al Hoss

Passing Data and objects between parent and child web forms
HTML, .NET (.NET 2.0), ASP.NET, Dev
Posted:23 Aug 2008
Views:6,648
Bookmarked:8 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 2.19 Rating: 2.82 out of 5
3 votes, 50.0%
1

2

3

4
3 votes, 50.0%
5

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 from the post the results back to the patent from.

Passing simple data between parent and child Form

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 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 it 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


Member

Occupation: Software Developer
Company: CME Offshore
Location: Lebanon Lebanon

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Aug 2008
Editor:
Copyright 2008 by Mohammad Al Hoss
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project