KeyLimeTie Blog

Databind to Custom Objects (C#)

By Brian Pautsch – 6/2/2005 8:54:00 PM. Posted to Applications.

Using custom objects is always a great practice, especially in n-tiered development where abstraction across the layers is crucial. All too often I see people (including myself) use custom objects to handle most of the logic, but then databind repeaters, datagrids, etc. with datasets, datatables and data readers. By doing so, the database and user interface are now tightly coupled and maintenance can become a headache later on.

Download code

What's the solution? Bind your UI controls with your custom objects. Here's how:

1. UserColl.cs - Create a class that defines your custom object (UserDetails)
Lines 6-34: A simple class with three members (Email Address, First name and Last Name)

2. UserColl.cs - Create a custom collection class that implements the IEnumerable interface (Users)
Line 37: Inherit System.Collections.IEnumerable interface
Line 40: Declare private ArrayList to store data
Lines 43-46: Return the enumerator from my ArrayList
Lines 49-109: Methods to add/update/retrieve objects from the collection

3. WebForm1.aspx - Implement into a WebForm


Lines 8-11: I added JavaScript that confirms the delete.
Lines 23-43: DataGrid TemplateColumn's - Notice that each item is pulled from the Container and is referenced by the member name.
Line 47: TemplateColumn for the Delete LinkButton - Coded this way so was can find it during the ItemBound event and add the JavaScript confirm function.

4. WebForm1.aspx.cs - Implement into the CodeBehind

Lines 47-52: On first load, call LoadNewUsers to, you guessed it, load new users into the object. In a real life scenario, this would be pulled from a database.
Line 55: For this example, I simply put the session's data into a Session object.
Lines 58-86: LoadNewUsers - Create 3 objects and load them with details...each being added to our collection class. At the end, store this in Session State (for this example).
Lines 94-99: Look familiar? Usually, you bind with a dataset, datatable, etc...this time you're binding with a custom object!
Lines 101-114: dgUsers_ItemCreated - just a little something extra. When DataBind is called, the ItemCreated event is caught here. For each item, I find the Delete LinkButton and and the JavaScript function as an "onclick" attribute.
Line 116-167: dgUsers_ItemCommand - Some examples on how to access the collection. When "View" is clicked, I retrieve the object from the collection by "index" (Line 124). When "Delete" is clicked, I retrieve the object from the collection by "Email Address". To do this, I first need to find the LinkButton (Line 140). Then I can retrieve the object (Line 144). Once I have the object, I can delete it (Line 146). Finally, I rebind the custom object to the DataGrid to reflect the changes (Line 163) and refresh the Session variable (Line 166). For this example, the rebind and session variable refresh should just be in the "Delete" if structure, but I had originally planned to have an "Edit" link.

Comments

Leave a Comment

Name:
Email:
URL:
Comment:
Security Code:
Type Security Code:

Photos on Flickr

More Photos »

Search Blog


Get Email Updates

Like what you read here at KeyLimeTie? Sign up for our email list!

Subscribe