Tải bản đầy đủ (.pdf) (9 trang)

Tài liệu Display Data Using the Repeater Control pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (32.57 KB, 9 trang )

5.5 Display Data Using the Repeater Control
I have heard that the Repeater control is a great control for displaying a read-only list of
values, including hyperlinks. How do I populate a Repeater control and take advantage of
templates to display the data the way I want it to look?
Technique
The Repeater control 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 the Repeater control not only to display a list, but
also to use a couple of 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
page as the first list. The hyperlink will take you to another page to display the second
list.
A main tool in the creation of the Repeater control 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 the data source that is specified
for the Repeater object. You will see how to bind the Repeater 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 using the
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 using the 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: Display Data Using the Repeater 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
using the Repeater control. This list is displayed below the regions and contains the
territories for the region clicked (see Figure 5.6).
Figure 5.6. Another Repeater control is utilized for this list of territories.

When you click on the hyperlinks in the list, another page displays, with yet another
Repeater control used to display the 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>

×