5.5 DisplayDataUsingtheRepeaterControl I have heard that theRepeatercontrol is a great control for displaying a read-only list of values, including hyperlinks. How do I populate a Repeatercontrol and take advantage of templates to displaythedatathe way I want it to look? Technique TheRepeatercontrol allows you to list data in various formats, such as bulleted or numbered. It relies on the use of templates to display information in a list format. This How-To shows you how to use theRepeatercontrol not only to display a list, but also to use a couple of different controls-a Button and HyperLink-to display another list usingtheRepeater control. The second list will be displayed usingthe button on the same page as the first list. The hyperlink will take you to another page to displaythe second list. A main tool in the creation of theRepeatercontrol is the use of templates. Use of Templates Templates are used within HTML and allow you to include controls in your ASP.NET Web server controls. Within a template, you can specify various details about the area for which you are creating a template. Following is a list of the templates: • HeaderTemplate • ItemTemplate • FooterTemplate • AlternatingItemTemplate • SeparatorTemplate You can get an idea of what each of the templates is used for by its name. Here is an example of the HeaderTemplate, used in this How-To: <HeaderTemplate> <font face="Arial Black" size="2">List of Regions </font> <br> </HeaderTemplate> These lines are literally used as a template for how you want the section to be laid out, as well as what data to display. For the ItemTemplate in this How-To, two controls are displayed: a button and a hyperlink, shown by this snippet of the HTML: <asp:Button runat="server" Text= '<%# DataBinder.Eval(Container.DataItem, "RegionID") %> ' /> <asp:HyperLink Runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.RegionDescription") %>' NavigateURL='<%# DataBinder.Eval(Container, "DataItem.RegionURL") %>' ID="HyperLink1"/> As the name implies, the DataBinder supplies data from thedata source that is specified for theRepeater object. You will see how to bind theRepeater object in step 3. Creating URLs On-the-Fly In the NavigateURL of the hyperlink that was created, you can see the column DataItem.RegionURL being used. This is not a column that is found in the Regions table in Northwind. This column is created within the SQL statement that is supplied to a data adapter, by the following line of code: odaRegions = New OleDb.OleDbDataAdapter("Select RegionID, _ 'wfrmHowto5_5b.aspx?RegID=' + Cast(RegionID as Char(2)) As RegionURL, RegionDescription From Region", BuildCnnStr("(local)", "Northwind")) The RegionURL consists of a Web Form name and the statement ?RegID=Cast(RegionID as Char(2)), which ASP.NET loads into the new page and passes to the RegionID of the page, letting the code within the page read the RegionID usingthe Request object, as displayed here: odaTer = New _ OleDb.OleDbDataAdapter(_ "Select TerritoryDescription From Territories Where RegionID = " & Request.Item("RegID"), _ BuildCnnStr("(local)", "Northwind")) You will see both the creation and utilization of the URL in the steps that follow. Programming Repeater Events by Using ItemCommand When you're using buttons, you can program the response for when the buttons are pressed by usingthe ItemCommand event. This event is raised for all the controls that are used in the Repeater. You will see an example of this in step 5. Steps Open and run the Visual Basic .NET-Chapter 5 solution. From the main page, click on the hyperlink with the caption How-To 5.4: DisplayDataUsingtheRepeater Control. You will then see a page open displaying a list of the regions (see Figure 5.5). Figure 5.5. This list includes both a button, displaying the RegionID, and a hyperlink, displaying the region description. If you click one of the buttons displaying the Region ID, then another list is displayed usingtheRepeater control. This list is displayed below the regions and contains the territories for the region clicked (see Figure 5.6). Figure 5.6. Another Repeatercontrol is utilized for this list of territories. When you click on the hyperlinks in the list, another page displays, with yet another Repeatercontrol used to displaythe territories for the region chosen (see Figure 5.7). 1. Create a Web Form. Then place the controls listed in Table 5.8 with the following properties set. Table 5.8. Property Settings Repeater and HyperLink Controls Object Property Setting Repeater ID repRegions Repeater ID repTeritories HyperLink ID hplReturnToMain NavigateURL wfrmMain.aspx 2. Switch to the HTML tab in the designer. By adding the repeaters and naming the repeaters in step 1, you will see a line of code that looks like this: 3. <asp:Repeater id="repRegions"runat="server"></asp:Repeater> Replace this line of code with the code displayed in Listing 5.13. This code lays out the templates that were described in the "Technique" section, as well as the button and hyperlink described. Note that the FooterTemplate, listed here, is not really used for anything. It was included so that you could see how to use it. It can be excluded. Listing 5.13 wfrmHowTo5_5a.aspx: HTML for the repRegions Repeater <asp:Repeater id="repRegions" runat="server"> <HeaderTemplate> <font face="Arial Black" size="2">List of Regions </font> <br> </HeaderTemplate> <ItemTemplate> <asp:Button runat="server" Text= '<%# DataBinder.Eval(Container.DataItem, "RegionID") %> ' /> <asp:HyperLink Runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.RegionDescription") %>' NavigateURL='<%# DataBinder.Eval(Container, "DataItem.RegionURL") %>' ID="HyperLink1"/> <br> </ItemTemplate> <FooterTemplate> </FooterTemplate> </asp:Repeater> 4. Next, replace the HTML code inserted for the repTerritories repeater with the code in Listing 5.14. After the HeaderTemplate is specified, a Label control displays the TerritoryDescription column. Listing 5.14 wfrmHowTo5_5a.aspx: HTML for the repTerritories Repeater <asp:Repeater id="repTerritories" runat="server"> <HeaderTemplate> <br> <font face="Arial Black" size="2">List of Territories </font> <br> </HeaderTemplate> <ItemTemplate> <asp:Label runat="server" Text= '<%# DataBinder.Eval(Container.DataItem, _ "TerritoryDescription") %> ' /> <br> </ItemTemplate> </asp:Repeater> 5. Add the code in Listing 5.15 to the Load event of the page. If the page is first being loaded, then the odaRegions DataAdapter fills thedata table called dtRegions. dtRegions is set as thedata source for repRegions, and DataBind method is called, binding theRepeater to thedata table. Listing 5.15 wfrmHowTo5_5a.aspx.vb: Loading the repRegions RepeaterControl Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here If Not Me.IsPostBack Then Dim odaRegions As OleDb.OleDbDataAdapter Dim dtRegions As DataTable odaRegions = New OleDb.OleDbDataAdapter("Select RegionID, _ 'wfrmHowto5_5b.aspx?RegID=' + Cast(RegionID as Char(2)) As RegionURL, _ RegionDescription From Region", BuildCnnStr("(local)", "Northwind")) dtRegions = New DataTable() odaRegions.Fill(dtRegions) repRegions.DataSource = dtRegions repRegions.DataBind() End If End Sub 6. Add the code in Listing 5.16 to the ItemCommand event of repRegions. This code takes the Text property of the button, which is the RegionID, and uses it in the SQL string to supply the odaTer data adapter. Next, odaTer fills dtTer, which is then used as thedata source for repTerritories. Listing 5.16 wfrmHowTo5_5a.aspx.vb: Loading the Categories DropDown and Product Table Control Private Sub repRegions_ItemCommand(ByVal source As Object, _ ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repRegions.ItemCommand Dim odaTer As OleDb.OleDbDataAdapter Dim dtTer As DataTable odaTer = New _ OleDb.OleDbDataAdapter(_ "Select TerritoryDescription From Territories Where RegionID = " & CType(e.CommandSource, Button).Text(), _ BuildCnnStr("(local)", "Northwind")) dtTer = New DataTable() odaTer.Fill(dtTer) repTerritories.DataSource = dtTer repTerritories.DataBind() End Sub 7. Create another Web Form. Then place a Repeatercontrol on it and set the ID of theRepeater to be repTerritories. Note When you are saving and naming the Web Form created in this step, make sure you name it the same as that used in the code in step 4. Remember that it was the Web Form used in the RegionURL. 8. Switch to the HTML tab in the designer. Replace the line of code that has been created for the repTerritories Repeatercontrol with the code shown in Listing 5.17. Listing 5.17 wfrmHowTo5_5b.aspx: HTML for the repTerritories Repeater on the Second Web Form <asp:Repeater id="repTerritories" runat="server"> <HeaderTemplate> <br> <font face="Arial Black" size="2">List of Terriories </font> <br> </HeaderTemplate> <ItemTemplate> <asp:Label runat="server" Text= '<%# DataBinder.Eval(Container.DataItem, _ "TerritoryDescription") %> ' ID="Label1" NAME="Label1"/> <br> </ItemTemplate> </asp:Repeater> 9. Add the code in Listing 5.18 to the Load event of the page. This code reads the RegionID first from the first Web Form usingthe Request object. Next, it is used to fill the dtTer data table, which is then assigned as thedata source for repTerritories. Listing 5.18 wfrmHowTo5_5b.aspx.vb: Loading the repTerritories RepeaterControl on the Second Web Form Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim odaTer As OleDb.OleDbDataAdapter Dim dtTer As DataTable odaTer = New OleDb.OleDbDataAdapter(_ "Select TerritoryDescription From Territories Where RegionID = " & Request.Item("RegID"), _ BuildCnnStr("(local)", "Northwind")) dtTer = New DataTable() odaTer.Fill(dtTer) repTerritories.DataSource = dtTer repTerritories.DataBind() End Sub Figure 5.7. A third Repeatercontrol is utilized for this list of territories on a new page. . different controls-a Button and HyperLink-to display another list using the Repeater control. The second list will be displayed using the button on the same. hyperlink, displaying the region description. If you click one of the buttons displaying the Region ID, then another list is displayed using the Repeater control.