Populating ListBoxes

I l @ ve RuBoard

Populating ListBoxes

One of the most common tasks you will encounter with using WebControls is populating their values from a data source. DropDownList , in particular, is one of the controls that most commonly needs to be populated from a data source of some kind. To wrap up this chapter, we're going to cover three different ways of populating DropDownList controls using ASP.NET. This example uses WebControls, but you could easily convert to using HtmlControls using the same syntax. The complete source code for the ASP page and codebehind is in Listings 5.3 and 5.4.

Listing 5.3 Populating drop-down lists (DropDownList.aspx).
 <%@ Page language="c#" Codebehind="DropDownList.aspx.cs" AutoEventWireup="false" Inherits="ASPAuthors.aspnetbyexample.ch05.DropDownList" Src="DropDownList.aspx.cs" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head>     <title>Populating DropDownLists</title> </head>   <body>     <form id="DropDownList" method="post" runat="server">     Declarative:     <asp:DropDownList id="DropDownList1" runat="server">         <asp:ListItem>One</asp:ListItem>         <asp:ListItem>Two</asp:ListItem>     </asp:DropDownList>     <br>     Programmatic Adding:     <asp:DropDownList id="Dropdownlist2" runat="server"></asp:DropDownList>     <br>     Programmatic DataBinding:     <asp:DropDownList id="Dropdownlist3" runat="server"></asp:DropDownList>      </form>   </body> </html> 
Listing 5.4 Populating drop-down lists (DropDownList.aspx.cs).
 private void Page_Load(object sender, System.EventArgs e) {     if(!Page.IsPostBack)     {         // Programmatic Adding         Dropdownlist2.Items.Add("One");         Dropdownlist2.Items.Add(new ListItem("Two","2"));         Dropdownlist2.Items.Insert(0,""); // Add a blank item to the top         // Create a data source         ArrayList AL = new ArrayList();         AL.Add("One");         AL.Add("Two");         // Set values using databinding         Dropdownlist3.DataSource = AL;         Dropdownlist3.DataBind();         // Set selected item to "Two"         Dropdownlist3.Items.FindByText("Two").Selected = true;     } } 

The result of this page is shown in Figure 5.6. Notice that the third control has "Two" as its selected item, because we set that in our last line of code in Listing 5.4.

Figure 5.6. Several ListBox controls.

This example shows three ways to add controls to a list control, in this case a DropDownList WebControl. These three techniques include declaratively adding items, programmatically adding items using the Items collection of the control, or databinding the control to a data source.

Declarative

The simplest way to populate a list control is to add ListItem tags ( <asp:ListItem>foo</asp:/ListItem> ) to the control itself on the web page. By using the properties of the ListItem ( Value , Text , and Selected ), you can also set the value as well as the text of the list item, or whether it is selected, just as you can do with HTML Option tags.

Programmatic with Items Collection

The second easiest way to build up a list of items in a DropDownList or ListBox control is to use the Items collection's Add or Insert method. The Add method takes either a string value or a ListItem object, both of which are shown in the example. Likewise, the Insert method accepts an integer index value (where 0 is the first item in the list) where the item should be inserted, and either a string value or a ListItem object for the item to add to the list. You can also use the Items collection to locate an item in the list and make it Selected (or not), using the FindByText or FindByValue methods of the Items collection.

Programmatic with Databinding

The most advanced way to populate a list of items (but also the easiest if you have a component returning a dataset or datareader) is to simply bind the DropDownList to a datasource using databinding. We looked at databinding some last chapter, and we will see more of it in the next chapter. In our example, we created an ArrayList simply to use as our data source. If you bind to an actual database, you might want to also set the DataTextField and DataValueField to the fields in the data source that correspond to what you want displayed (for example, an ID column and the corresponding text associated with that ID).

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net