ASPAlliance.com: The #1 ASP.NET Community
The ASPSmith
Search
D: | Domains | Authors.aspalliance.com | Stevesmith | Articles | ASP.NET DropDownList Web Control, Part 2
ASP.NET DropDownList Web Control, Part 2

By Steven Smith

[Example]

Continuing on from where we left off, let's look at some more ways to populate a list control, such as a DropDownList. We saw in Part 1 how to add items to a DropDownList declaratively (using tags), now let's see how we would do it in our ASP.NET's code. Specifically, we will handle this in the Page_Load event.

void Page_Load(){
    if(!Page.IsPostBack){
        candies.Items.Add("");
        candies.Items.Add(new ListItem("M&M Chocolate Candies","M&Ms"));
        candies.Items.Add("Snickers");
        candies.Items.Add("Reese's Pieces");
        candies.Items.Add(new ListItem("","Hidden Value!!!"));
    }
}

You can test this code here. Basically, all List-type Web Controls have a property called Items, which is a collection of the list items held within that control. To add an item to the end of the control's list, you use the Add method. The Add method has several overloaded versions, including one that just takes a string (as in the first line, which adds a blank item), and one that takes a ListItem (as we used for M&Ms and the last item). Note that we enclosed all of this logic in an if(!Page.IsPostBack) structure -- this is because the control handles its own viewstate, and thus will retain its list of values on subsequent submissions. By placing list binding logic within the if(!Page.IsPostBack) section of the Page_Load event handler, especially for database-populated lists, we greatly improve the performance of our page.

All of the techniques we have seen so far for adding items to a list control have been sort of manual. In order to add N items, we need to write N lines of code. Obviously this won't work too well for very large lists, which is why we turn now to the concept of Data Binding. Many of the ASP.NET built-in controls support Data Binding, through which a control's state can be populated from a data source. In the case of list controls, the contents of the list can be pulled from a data xource to eliminate much of the work involved in populating the list. Here is an example of using an ArrayList to populate another Candy DropDownList:

       ArrayList AL = new ArrayList();
        AL.Add ("Butterscotch");
        AL.Add ("Peppermint");
        AL.Add ("Cinnamon");
        sweets.DataSource = AL;
        sweets.DataBind();

In this example, we create an ArrayList called 'AL' and add three items to it. Then, we use the List control's DataSource property to assign the ArrayList as the datasource of the DropDownList, and finally we call the DropDownList's DataBind() method. This code also would go inside of your Page_Load event handler, within an if(!Page.IsPostBack) section.

Populate DropDownList from Data Query (coming soon - it looks a LOT like binding to an array, I just don't have a working example ready yet)

After you have your DropDownList from a DataBind() operation, you may want to add a blank Item to the top of the list to serve as a default (especially if you want to use a RequiredFieldValidator and you want the default to be blank). To do this, we need to use a different method of the List control, the Insert method. Adding a blank item to the top of our sweets DropDownList would be done like this:

sweets.Items.Insert(0, new ListItem());

Finally, to set a particular item as the default, such as if you were wanted to pre-set an item based on the results of a database query, you would do the following:

        AL = new ArrayList();
        AL.Add ("Glazed");
        AL.Add ("Sprinkles");
        AL.Add ("Jelly");
        donuts.DataSource = AL;
        donuts.DataBind();

        donuts.Items.Insert(0, new ListItem());

        donuts.Items.FindByValue("Sprinkles").Selected = true;

Within my Page_Load event, within an if(!Page.IsPostBack) block, we have added this code to populate a new DropDownList of donuts. We have determined that "Sprinkles" is the default item, and so we use the FindByValue method of the DropDownList to located the index of this item in the list, and set its Selected property to true. Note that since FindByValue is a method and not a handle to a collection, we use () and not []. If necessary, you can also use the FindByText method. Also note that since this code only executes when the page is first loaded, the user can change this value and their changes are retained on future visits to the page.

Other Links




ASP.NET Developer's Cookbook, By Steven Smith, Rob Howard, ASPAlliance.com 

ASP.NET By Example, By Steven Smith 




Steven Smith, MCSE + Internet (4.0)
Last Modified: 6/12/2009 10:58:21 AM
History: 6/12/2009 10:58:21 AM