|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Friday, March 24, 2006Binding a ListControl to an ArrayList(chapter 3)In most of our previous examples, we loaded a list box via code in the Page_Load event. In this section, we will introduce an important concept of a typical ASP.NET development practice. Rather than populating a specific control via code, we may bind a control to a data source (something that contains data). In this case, the control will automatically assume the value or values contained in the data source. At this stage, you may not see the benefit of this approach, but it will shine like a jewel when we learn how to display and manipulate data from databases. In the example shown in Figure 3.36 and Figure 3.37, we have shown a similar example of binding an HtmlSelect control to a SortedList). Since the ArrayList object is also very common in ASP.NET framework, we will bind our ListControl to an ArrayList in our next example.
Often we create and load a collection of objects into certain structures.These structures are known as collection objects. For example, an ArrayList is a collection object. It is actually very similar to a dynamic array of objects. Suppose that one of these ArrayList objects contains the names of some flowers. If needed, we may bind one or more controls to this ArrayList.That way, the controls will be automatically loaded with the values in the ArrayList. Don’t worry! We will not deprive you of binding controls to databases.Those examples will appear later in this chapter. Binding a control to a data source is very simple. Rather than developing a data loading procedure, we just set the DataSource property of a control to a data source.Then we employ the DataBind() method of the control to accomplish the binding task. In our example, we will first create an ArrayList of flowers, and then we will bind a list box (lstFlower) with the ArrayList. Figure 3.48 shows the runtime view of the application.The complete listing of the code is shown in Figure 3.49. Figure 3.48 Binding a ListControl to an ArrayList Figure 3.49 Complete Listing of DataBind1.aspx <!— Chapter3\DataBind1.aspx —> <%@ Page Language="VB" Debug="true" %> <html><head></head><title></title><body> <form runat="server"> Select a flower, and then click the submit button please:<br> <asp:ListBox id="lstFlowers" runat="server" rows="3" AutoPostBack="True" onSelectedIndexChanged="showSelection"/> </asp:ListBox><br><br> <asp:Label id=lblMessage runat="server"></asp:Label></p> </body></form></html> <script language=vb runat="server"> Sub Page_Load(source As Object, e As EventArgs) If Not Page.IsPostBack Then Dim myArrayList As New ArrayList ' Populate the ArrayList: This will be a data source myArrayList.Add("Azalea") myArrayList.Add("Tulip") myArrayList.Add("Rose") ' Step 1: Specify the Datasource property of the list control lstFlowers.DataSource= myArrayList ' Step 2: Employ the DataBind() method to load the ' list control from its DataSource automatically lstFlowers.DataBind() lstFlowers.SelectedIndex=0 End If End Sub Sub showSelection(sender As Object, e As EventArgs) lblMessage.Text="You have selected "+lstFlowers.SelectedItem.Text End Sub </script> |
0 Comments:
Post a Comment
<< Home