Tip: Populating dropdown lists with AC7 data

This forum is where we'll mirror posts that are of value to the community so they may be more easily found.
Post Reply
User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Tip: Populating dropdown lists with AC7 data

Post by AbleMods » Wed Jan 28, 2009 10:12 pm

Introduction
Well it's been a little while since I wrote an article, so I thought I'd travel a different path for a while and post some of my coding/programming tips. I've been a programmer for over 25 years, mostly in the dBase world starting with Clipper/dBase II and continuing to today with ASP.Net 2.0. One thing that continues to impress me with ASP.Net is how many different ways there are to accomplish a given task. Great if you're young and have a keen memory - not so great when you're getting long in the tooth and short in the memory :)

So I've had to rely on some good habits I developed long ago. Consistency. Keeping your development style consistent over time will have huge rewards for you down the road. This can be difficult to do at first, especially if you're just getting wet-behind-the-ears with .Net coding. You're in a learning mode, so how you did it last week won't necessarily be how you do it next week.

But eventually you will narrow your style. Your path to honing your style should at least include improving readability, maintaining a logical flow and allow for modification without a full rewrite. Again, easier said than done but experience is the best teacher. Always keep those three basic principles in mind as you develop and you will improve, I promise.

Populating Dropdown List Controls
Often I find myself needing to populate a dropdown list with values from an AC7 table. For today's example, let's say we've been given a programming task. That task is to present the user with a dropdown list of choices from an AC7 table. To be more specific, we need to let the user tell us (the programmer) which Email Template they would like to use.
The important thing to remember with programming is that everything is designed in "layers". There is the presentation layer which is nothing more than the frontend that the user will see. But there's also the developer layer - that's our turf and we programmers tend to look at things a little differently.

In our example task, we want the "presentation" layer to be easy and readable English text for the user. But on the backend developer side, we want to work with small numeric values.

Dropdown list controls make that simple to do. Each item on the list can have a "Text" value that represents what is displayed to the user and a "Data" value that is assigned to the control once a selection is made. This helps alot and follows the layer concept - users get easy-to-read text and we get easy-to-use data values.

As is the nature of .Net programming, there is more than one way to accomplish this task. First, I'm going to show you a simple loop technique. Then we'll do something a little more advanced with the datasource property.

To start, you need a dropdown control on your ASPX page with an ID of "list_TemplateId". I always prefix my control ID values with the type of control - that way I don't have to examine the control just to tell what it is. I use "chk_" for checkboxes, "txt_" for text boxes, "lbl_" for label controls and so forth.

The list_TemplateId control doesn't need to have any list items added on the ASPX page. We're going to do all of that in the code-behind page.

First, let's use technique 1: The Loop

Code: Select all

            ' load up templates for the list
            Dim _Templates As EmailTemplateCollection = EmailTemplateDataSource.LoadForStore()
            list_TemplateId.Items.Add(New ListItem("- None -", ""))

            For Each _EmailTemplate As EmailTemplate In _Templates
                list_TemplateId.Items.Add(New ListItem(_EmailTemplate.Name, _EmailTemplate.EmailTemplateId))
            Next
Nice. Only 6 lines of code but a whole lot is happening for you. First, we create a collection of all the email templates in the store. We do this by calling the LoadforStore() method on the email template data source.

Next we make sure the first choice in the list is a generic "none" choice - that way the user has the option of not selecting anything. You'll know this in the developer layer because the SelectedValue for the list_TemplateId will be empty/blank.

Once we've got the empty choice added, we start a clean little loop. The For..Each command will step through each template stored in the _Templates collection and a new ListItem will be added. The name will be used for the "presentation" layer and the EmailTemplateId value will be used for the "developer" layer. Once the user has made a choice, the list_TemplateId.SelectedValue will be equal to the EmailTemplateId value of the chosen email template. Nifty!

Simple, effective and darn easy to read. That's what I like.

The DataSource Method
Ok so here's where things get cool but also are a little less flexible. You'll notice above we were able to add an item to the list before the actual email templates were loaded up. Using this next technique with the DataSource property, that won't be possible. Whatever list items established in the control before will be wiped out when the datasource is established.

Here's how to do the same thing above using the DataSource method:

Code: Select all

            ' Load up the collection with all the store templates
            Dim _Templates As EmailTemplateCollection = EmailTemplateDataSource.LoadForStore()

            ' Setup dropdown list
            list_TemplateId.DataSource = _Templates
            list_TemplateId.DataTextField = "Name"
            list_TemplateId.DataValueField = "EmailTemplateId"
            list_TemplateId.DataBind()
Or you want to *really* get fancy, do this:

Code: Select all

            ' Setup dropdown lists
            list_Templates.DataSource = EmailTemplateDataSource.LoadForStore()

            list_Templates.DataTextField = "Name"
            list_Templates.DataValueField = "EmailTemplateId"
            list_Templates.DataBind()
Either way, just be sure that you always call DataBind() after you establish the datasource. Otherwise .Net will not see you've changed the source of the items in the control and the control will render empty/blank.

Notice how the DataSource method has eliminated all looping. And it's make it incredibly clear which field will be displayed on the presentation layer and which will be supplied to you on the developer layer.

Conclusion
Which style is better? Realistically, it doesn't really matter for your purposes. From a pure speed perspective, the DataSource method will be quicker. But that's just not going to be a factor for you unless A) the table you are pulling from has thousands of records or B) you're trying to populate 47 different dropdowns on the same page. Of course in either case, you've already realized your going down a terrible design path and rethink the logic before the mistake is complete :wink:

In the end, what matters most is that your code is solid and you can read it without being prone to confusion or error. Go with what works best for you and keep those three principles of readability, logical flow and enhancement planning in mind.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

Post Reply