Binding Profile properties using ObjectDataSource

Onion Blog

Syndication

I recently had the need to display a collections of items stored in profile in a Repeater control (let's say the collection was a shopping cart with purchase items), and after thinking about it, realized that there was no obvious way to hook up a collection stored in profile using the ObjectDataSource. I resorted to binding programmatically, which is of course always an option:
 
protected void Page_Load(object sender, EventArgs e)
{
  itemsRepeater.DataSource = Profile.ShoppingCart.Items;
  itemsRepeater.DataBind();
}
 
Once you get used to binding things declaratively, however, it just feels out of place to resort to programmatic binding. To do this declaratively, I decided to write a static helper class to bind to, a technique which I think others might find useful, hence this blog entry. I called the class ProfileBinder, and added it to the same source code file where I had defined my custom ShoppingCart and Item classes (placed in App_Code). I then used HttpContext.Current.Profile to retrieve the current profile, cast it to the ASP.NET-generated ProfileCommon class (this is a strongly-typed derivative from ProfileBase that is created from the profile settings in your web.config file) and I now had a class ready for consumption by an ObjectDataSource:

 

public static class ProfileBinder
{
 
public static List<Item> GetShoppingCartItems()
  {
   
return ((ProfileCommon)HttpContext.Current.Profile).ShoppingCart.Items;
  }
}

The final snippet is what my repeater and data source controls looked like with this new class:
 
<asp:Repeater runat="server" ID="_itemsRepeater" DataSourceID="_itemsDataSource">
  <ItemTemplate>
    <li><%# Eval("Description") %> - <%# Eval("Cost", "{0:c}") %></li>
  </ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource runat="server" ID="_itemsDataSource"
      
SelectMethod="GetShoppingCartItems" TypeName="ProfileBinder" />
 

Posted Oct 24 2005, 11:33 AM by fritz-onion
Filed under:

Comments

Daniel Fisher(lennybacon) wrote re: Binding Profile properties using ObjectDataSource
on 10-25-2005 3:36 AM
Nice post!
According to kcwalina
<http://blogs.msdn.com/kcwalina/archive/2005/09/26/474010.aspx> you should use Collection<> instead of List<>
Fritz Onion wrote re: Binding Profile properties using ObjectDataSource
on 10-25-2005 3:51 AM
That makes sense - thanks for the link!
Christopher Steen wrote Link Listing - October 25, 2005
on 10-25-2005 9:25 AM
2005 Salary Survey Snapshots [Via: editor@computerworld.com (Computerworld Editor) ]
ANN: New VistaDB...
Poul Foged Nielsen wrote re: Binding Profile properties using ObjectDataSource
on 11-02-2005 4:52 AM
Well, I'd recommend using ICollecion<> instead of Collection<>, hiding away your collecion-decision. Then use whatever suits you inside the function...
Sahil Malik [MVP C#] wrote How to: Implement Paging in asp:Repeater for your Custom Business Obejcts
on 12-09-2005 7:10 PM
First of all, check this song out. That's what's been on my Rewind Play for the past 1 hour as I've been...
Peter wrote re: Binding Profile properties using ObjectDataSource
on 02-08-2007 12:19 AM
In this type of scenario, although not specifically to do with Profile info, is it possible to get the ObjectDataSource to call a method such as

public static void UpdateItems(List<Item> updatedItems) {}

The updatedItems list containing changes made in controls in a repeater whose DataSourceID is the ObjectDataSource control?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?