Friday, May 31, 2013

Adding items to a drop down list in a ListView (or a GridView for that matter)

I’m in the process of creating a simple “product listing” in an ASP.NET website, just to show some products.. like t-shirts.. display a drop down list with the available sizes, and provide an “Add” button to add to a cart, much like this:

Capture

Even though it’s possible this application will barely get any usage within the next 6 months, and will probably display no more than 5 items at a time (it’s for internal company usage), I nonetheless decided I must make my code as efficient as possible.  I took to using a repeater control to iterate through an XmlDataSource and display my product data.  Within an hour, I had this working (hooray!).  Then, I added some form controls (buttons and drop down lists) with dummy data and quickly found that a repeater was a poor choice, as EVERY form control was submitted when the page performed a POST to the server (boo!). [Note: I really don’t understand why this happened.. I didn’t really find anything on this, or research it much, I just tried using a different control on a hunch, and it worked..]

Learning my lesson, I switched over to using a ListView control to instead display my data.  I was still able to use an XmlDataSource for my product info, and now clicking an “Add” button for a product added only that product, not submitting for every form control.  Now, all that was left to do was to bind actual data to my form controls, rather than use the pre-existing dummy data.  This was surprisingly difficult to do.

The task at hand was to edit the “product sizes” drop down list and provide appropriate values based as a comma delimited value in my XML file.  Namely, if the product was “Shirt 1”, and the sizes were “S,M,L”, then S, M and L should all be separate items in the drop down list.  The problem was, it was hard to find out *what* data was binding at any given time.

Fortunately, the solution was simple, I really think I was just not Googling properly.  To add items to the drop down list, I had to use the ItemDataBound event on my ListView control.  At that time, I had to check for my drop down list using the FindControls method, and then use the DataBinder.Eval Function to pull the data for that current row.  As I said, very simple, just not well documented (for the terms I was searching for at least ;)

Anywho, you read this far, the least I can do is give you a code sample as well.  Hope this helps!

            If e.Item.FindControl("ddlSize") IsNot Nothing Then
            'Get our dropdownlist
            Dim ddl As DropDownList = CType(e.Item.FindControl _
            ("ddlSize"), DropDownList)
    
            'Make sure we didn't already add items to this ctrl
            If ddl.Items.Count = 0 Then

                'get our sizes, which are stored in comma delimited format
                Dim sizes() As String = DataBinder.Eval _
                (e.Item.DataItem, "Sizes").ToString.Split(",")
               
                'add our items
                For Each s As String In sizes
                    ddl.Items.Add(New ListItem(s, s))
                Next

            End If
     End If

No comments:

Post a Comment