Solved

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

Posted on 2009-05-05
5
2,668 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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