YOMEDIA
ADSENSE
Display Data Using the Repeater Control
125
lượt xem 16
download
lượt xem 16
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
5,5 Hiển thị dữ liệu Sử dụng Repeater Control Tôi có nghe nói rằng việc kiểm soát Repeater là một điều khiển tuyệt vời cho hiển thị một danh sách chỉ đọc các giá trị, bao gồm các siêu liên kết. Làm thế nào để cư một điều khiển Repeater và tận dụng các mẫu để hiển thị các dữ liệu cách tôi muốn nó nhìn?
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Display Data Using the Repeater Control
- 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: List of Regions 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:
- Text= ' ' /> 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. 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 List of Regions 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 List of Territories
- 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, _
- 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 List of Terriories
- 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.
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn