Strongly-Typed Binding for List Controls
page 3 of 5
by J. Ambrose Little
Feedback
Average Rating: 
Views (Total / Last 10 Days): 26394/ 101

Binding to List Controls

[Download Sample Project]

Another feature that I thought to add is to simplify binding the collection to list controls, such as the DropDownList, RadioButtonList, etc.  In order to do this, I added a method called BindToListControl that takes a reference to the list control the user wants to bind, sets its DataSource,  DataTextField, and DataValueField properties appropriately, and calls the DataBind method on the control to bind the collection to the control.

Listing 3


1:     /// <summary>

2: /// Bind the given <i>list</i> to this item collection.
3: /// </summary>
4: /// <param name="list">List control to bind to.</param>
5: public void BindToListControl(System.Web.UI.WebControls.ListControl list)
6: {
7: list.DataSource = this;
8: list.DataTextField = "Name";
9: list.DataValueField = "Value";
10: list.DataBind();
11: }



 


View Entire Article

User Comments

Title: Well done. Very useful   
Name: Leo
Date: 2009-02-24 8:31:10 AM
Comment:
Hi J. Ambrose Little,

Thanks for your code. I found it very useful indeed.
I used it in a scenario where I needed to create a NameValuePair Collection to concatenate two fields before displaying it on a DropDownList. Without your approach I would have to retrieve an concatenate column field just for my business object in one specific dropdown.

I am using CSLA as framework for the Business Objects. I am looking forward to make it generic with my objects as is not suitable for me use your FillFromDb, because this data is already manipulated by my objects in a business layer.
Unfortunatelly I could not understand the BuildingParamters as I found quite hard to understand events with delegate, etc.

If you have experience with CSLA and have any suggestion to make the Fill up in a generic manner, please let me know.

Thanks a lot.






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


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-08-19 8:28:17 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search