Solved

ASP.NET - C# - Pass Custom Object Between Page and Pop Up Page

Posted on 2009-05-05
5
2,638 Views
Last Modified: 2013-11-07
I am working on a data entry application.  I am using a FormView which is DataBound to an ObjectDataSource.  The ObjectDataSource returns a custom object.

For one of the fields, I need to use a PopUp window, which will contain controls to allow users to select from a list of values.

I am looking for a good mechanism for passing a complex object back AND forth betwen the base page and the PopUp window.  Additionally, I do NOT want to use Session (My production envioronment is set up as a Web Farm, and Session is not shared between web servers).

For those fimiliar with MOSS 2007 (SharePoint), MOSS offers similiar functionality with the People Picker control.  I believe the PeoplePicker uses a hidden input field to store a serialized copy of a DataSet for passing between pages.  
Could anyone help me code that out?

Some example Pseduo-Code below.  Stepping through it, DataEntryPage.aspx, hosts MyCustomControl.  MyCustomControl receives an instance of a List<int> (although this could modified to a more complex object later on).  MyCustomControl has an Edit button.  When clicked, EditPopUpWindow.aspx needs to open, receive a copy of MyCustomControl.IDs, perform some processing on it (in code-behind) and then present in on the GUI.  We can assume EditPopupWindow.aspx contains controls to allow users to modify IDs.  Finally, when the EditPopupWindow.aspx's Save button is clicked, I need to pass the Updated IDs back to MyCustomControl and initiate a post back so MyCustomControl can re-DataBind.

Thanks in Advance!!!!!

PJ


///////////////////////////////////////////////////////
public class MyDataObject
{
public string Title{get;set;}

private List<int> m_IDs = new List<int>();
public List<int> IDs{ get{ return m_IDs; } }
}


///////////////////////////////////////////////////////
public class ProviderForObjectDataSource
{

public List<MyDataObject> Select()
{
//Retrieve MyDataObjects from persistant storage
return new List<MyDataObject>();
}

public void Insert(MyDataObject newDataObject)
{
//Add newDataObject to persitant storage
}

}

///////////////////////////////////////////////////////
//DataEntryPage.aspx
///////////////////////////////////////////////////////
<%@Page Language="C#" CodeBehind="DataEntryPage.aspx.cs" %>
<%-- Register uc1:MyCustomControl --%>
<html>
<body>
<form id="form1" runat="server">
<asp:FormView id="FormView1" runat="server" DataSourceID="MyObjectDataSource" DefaultMode="Insert" >
<InsertItemTemplate>
Title: <asp:TextBox id="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
IDs:   <uc1:MyCustomControl id="MyCustomControl1" runat="server" IDs='<%# Bind("IDs")%>'
</InsertItemTemplate>
</asp:FormView>
<asp:ObjectDataSource id="MyObjectDataSource" runat="server"
      SelectMethod="Select" InsertMethod="Insert"
      TypeName="ProviderForObjectDataSource"
      DataObjectTypeName="MyDataObject" />

</form>
</body>
</html>
///////////////////////////////////////////////////////
//MyCustomControl.ascx
///////////////////////////////////////////////////////
<%@Control Language="C#" CodeBehind="MyCsutomControl.ascx.cs" %>

<%-- IDRepeator DataSource set in codebehind --%>
IDs: <asp:Repeator ID="IDRepeator" runat="server" />

<asp:Button ID="OpenEditPopUpWindowButton" runat="server" OnClick="OpenEditPopUpWindowButton_Click"  />
///////////////////////////////////////////////////////
//MyCustomControl.ascx.cs
///////////////////////////////////////////////////////
public partial class MyCustomControl : System.Web.UI.UserControl
{

private List<int> m_IDs = new List<int>();

public List<int> IDs
{
      get{ return m_IDs; }
      set{
            m_IDs = value;
            IDRepeator.DataSource = m_IDs;
            IDRepeator.DataBind();
      }
}

protected void OpenEditPopUpWindowButton_Click(object sender, EventArgs e)
{
//HOW DO I OPEN EditPopUpWindow AND PASS IT this.IDs?
//CAN'T USE Sesssion AND QueryString PROBABLY WONT BE FEASIBLE
}

}
///////////////////////////////////////////////////////
EditPopUpWindow.aspx
///////////////////////////////////////////////////////
<%@Page Langugage="C#" CodeBehind="EditPopUpWindow.aspx.cs" %>

<%-- Controls For Working with IDs --%>
<asp:Button ID="Save" runat="Server" OnClick="Save_Click" />
///////////////////////////////////////////////////////
EditPopUpWindow.aspx.cs
///////////////////////////////////////////////////////
public partial class EditPopUpWindow : System.Web.UI.Page
{
private List<int> m_IDs = new List<int>();

protected void Page_Load(Object sender, EventArgs e)
{
//I NEED IDs TO BE POPULATED AT THIS POINT, OR A LATER
//ASP.NET LIFECYCLE EVENT, SO I CAN DO SOME PROCESSING ON IT
//BEFORE BINDING IT TO THE UI.
}

protected void OpenEditPopUpWindowButton_Click(object sender, EventArgs e)
{
//HOW DO I PASS IDs BACK TO MyCustomControl AND FORCE MyCustomControl TO
//DO A POSTBACK, SO IT CAN UPDATE IT'S GUI WITH THE NEW IDs?
}

public List<int> IDs
{
      get{ return m_IDs; }
      set{
            m_IDs = value;
            IDRepeator.DataSource = m_IDs;
            IDRepeator.DataBind();
      }
}
}
///////////////////////////////////////////////////////




0
Comment
Question by:ppittle
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:tetorvik
ID: 24311570
Would it be an alternative to not open a real popup window, but opening a ASP.NET Ajax modal popup instead.
Then there would be no need to pass the objects between pages.
See a sample implementation where a gridview is extended with editing form.
http://mattberseth.com/blog/2008/04/masterdetail_with_the_gridview.html
(The scenario is a bit different, but I think the same approach could fit also to your scenario)

0
 
LVL 8

Author Comment

by:ppittle
ID: 24326240
Tetorvik,

I reviewed that example, it makes use of AJAX, specifically the Ajax Toolkit add-on, which requires .NET 3.5.  I'm restricted to using .NET 2.0.  

Any other ideas that would fit this constraint?
0
 
LVL 9

Accepted Solution

by:
tetorvik earned 500 total points
ID: 24329208
There is also a toolkit version available for .NET 2.0, which contains modal popup
http://www.codeplex.com/AjaxControlToolkit/Release/ProjectReleases.aspx?ReleaseId=11121

see also an alternative implementation using jQuery instead of Ajax Toolkit.
http://mattberseth.com/blog/2008/06/masterdetail_with_the_gridview_1.html
0
 
LVL 8

Author Comment

by:ppittle
ID: 24330459
The jQuery tutorial looks interesting, let me take a look at it and get back to you.
0
 
LVL 8

Author Closing Comment

by:ppittle
ID: 31578150
Ended up going with the Modal Popup using the AJAX Extensions for ASP.NET 2.0 and the AjaxToolkit.  Worked great!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now