Databinding to Custom Objects
page 3 of 4
by J. Ambrose Little
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25881/ 24

Usage the First Part: The Page Class

1:     /// <summary>

2:
/// Where the knights meet the old man from Scene 24...
3:
/// </summary>
4: public class TheBridge : System.Web.UI.Page
5: {
6: protected System.Web.UI.WebControls.DataGrid BridgeMaster;
7:
8: private void Page_Load(object sender, System.EventArgs e)
9: {
10: if (!this.IsPostBack)
11: this.BindGrid();
12: }
13:
14: private void BindGrid()
15: {
16: KnightOfRoundTable arthur, galahad, bedevere;
17: KnightsOfTheRoundTable roundTable =
new KnightsOfTheRoundTable();
18: arthur = new KnightOfRoundTable();
19: arthur.Name = "King Arthur";
20: arthur.Color = System.Drawing.Color.Gold;
21: arthur.Quest = "To seek the Holy Grail.";
22: roundTable.Add(arthur);
23:
24: galahad = new KnightOfRoundTable();
25: galahad.Name = "Sir Galahad";
26: galahad.Color = System.Drawing.Color.Yellow;
27: galahad.Quest = "Oooaaaaaah!";
28: roundTable.Add(galahad);
29:
30: bedevere = new KnightOfRoundTable();
31: bedevere.Name = "Sir Bedevere";
32: bedevere.Color = System.Drawing.Color.Blue;
33: bedevere.Quest = "Determine the flight velocity
of an unladen African (or European) swallow.";
34: roundTable.Add(bedevere);
35:
36: this.BridgeMaster.DataSource = roundTable;
37: this.BridgeMaster.DataBind();
38: }
39:
40: #region Web Form Designer generated code
41: override protected void OnInit(EventArgs e)
42: {
43:
//
44:
// CODEGEN: This call is required
by the ASP.NET Web Form Designer.

45:
//
46: InitializeComponent();
47: base.OnInit(e);
48: }
49:
50:
/// <summary>
51:
/// Required method for Designer support - do not modify
52:
/// the contents of this method with the code editor.
53:
/// </summary>
54: private void InitializeComponent()
55: {
56: this.Load += new System.EventHandler(this.Page_Load);
57:
58: }
59: #endregion
60: }

Clearly, the workhorse on this page is the BindGrid method (Lines 14-38). In this, you can see that I create three KnightOfRoundTable objects and name them Arthur, Galahad, and Bedevere respectively, assigning each attributes proper to their characters.  I also create my KnightsOfTheRoundTable collection and add each of these knights to it.  And lastly, on Lines 36-37, I set my KnightsOfTheRoundTable instance to be the DataSource for my BridgeMaster DataGrid and call DataBind on that grid.  And, not entirely insignificantly, I make a call in the Page.Load handler to BindGrid if this is not a post back, on Lines 10-11.

 


View Entire Article

User Comments

Title: help   
Name: Francisco Cruz
Date: 2009-12-17 3:11:43 PM
Comment:
Object Persistance between postbacks
Title: Very helpful   
Name: Ben
Date: 2008-09-15 2:22:35 PM
Comment:
just what I needed thanks
Title: Mr   
Name: Phillip Knezevich
Date: 2007-02-06 3:45:56 PM
Comment:
It's a geeky example, but I found it useful.
Title: Serialization   
Name: Ambrose
Date: 2006-09-21 7:49:08 PM
Comment:
Russ B,

I guess you're trying to use XML serialization like in a Web Service or directly? If so, yeah, it needs an Add method for when it deserializes so that it can add the deserialized instances to the new collection. You just need to implement an Add method, as far as I recall.
Title: Serialization   
Name: Russ B
Date: 2006-09-20 4:25:10 PM
Comment:
This is exactly what I've been looking for too.

I tried to serialize your KnightsOfTheRoundTable class and got this error: "System.InvalidOperationException: You must implement the Add(System.Object) method on BizDataBinding.KnightsOfTheRoundTable because it inherits from IEnumerable."

But you did implement Add. Any idea what else needs to be implemented?

...I'm still in 2003
Title: Binding to Complex Properties   
Name: J. Ambrose Little
Date: 2005-03-08 9:36:56 AM
Comment:
Hi Mark,

What I was saying is that if you just bind to the Color property, it will print out something like "Color [Black]" instead of the color name, which is really what we're after in this example. So in order to print out the value you want, just access the property of the property that you want to display, e.g., Color.Name. I hope this helps.
Title: Thanks!   
Name: Mark Miller
Date: 2005-03-08 12:27:37 AM
Comment:
I was looking around the Internet for an article like this. I'm a bit surprised it was not easier to find.

After finishing my first big web project, I wish I had used a technique like this. Unfortunately too many databinding examples online and in the MSDN docs just show you how to bind to a dataset.

I am a bit confused about the last part, where you say, "So if you do not have control over the ToString method on your class members' types, you may need to use this syntax to specify how you want the bound item to render that member." It seems you're referring to the string "Color [ColorName]", "ColorName" being the name of the color, but I don't know what this syntax would mean in relation to databinding. Aren't you trying to extract the name of the color so that it can display in whatever you are databinding to? What does the "Color [ColorName]" syntax accomplish?
Title: Using CollectionBase   
Name: Scott M
Date: 2004-12-12 4:19:12 PM
Comment:
Hey J,

Thanks for the article. I found it really useful. I read (in hardcopy) and article about inheriting from CollectionBase the other day and thought I'd let you know, turns out by your comment that you found it too!

So anyway, cheers and roll on generics,
Scott.
Title: Using CollectionBase   
Name: J. Ambrose Little
Date: 2004-08-31 12:53:30 PM
Comment:
Glad the article helped. I've actually found (since this article was published) that inheriting from CollectionBase is even easier than just implementing IEnumerable for my strongly-typed collections. Now I can't hardly wait for 2.0 where we won't even need to bother declaring our own collections and can use the generic ones instead!
Title: Thanks   
Name: Scott
Date: 2004-08-31 1:37:07 AM
Comment:
Hey Mr Little, great article. I was a little confused about implementing IEnumerable but once I dissected your example I managed to get my productset (using a dataset stored internally) going to bind it to a asp:repeater... I implemented ienumerator too but your article got me started, so cheers for putting it online.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-16 9:09:30 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search