1. Trang chủ
  2. » Công Nghệ Thông Tin

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

9 457 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Display data using the Repeater control
Định dạng
Số trang 9
Dung lượng 32,57 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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.. This How-To shows

Trang 1

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"

Trang 2

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

Trang 3

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

Trang 4

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

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

Trang 5

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>

Trang 6

</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 the data table called

dtRegions dtRegions is set as the data source for repRegions, and DataBind method is called, binding the Repeater to the data table

Listing 5.15 wfrmHowTo5_5a.aspx.vb: Loading the repRegions Repeater Control

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 the data 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, _

Trang 7

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 Repeater control on it and set the ID of the Repeater 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 Repeater control 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>

Trang 8

</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 using the Request object Next, it is used

to fill the dtTer data table, which is then assigned as the data source for

repTerritories

Listing 5.18 wfrmHowTo5_5b.aspx.vb: Loading the repTerritories Repeater Control 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 Repeater control is utilized for this list of territories on a new

page

Ngày đăng: 14/12/2013, 20:16

TỪ KHÓA LIÊN QUAN