ASP.NET 4 Unleased - p 61 doc

10 226 0
ASP.NET 4 Unleased - p 61 doc

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

Thông tin tài liệu

ptg 574 LISTING 12.4 ShowEmptyDataText.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Show Empty Data Text</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:DetailsView id=”dtlMovies” DataSourceID=”srcMovies” EmptyDataText=”<b>No Matching Record!</b>” Runat=”server” /> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies WHERE Id=-1” Runat=”server” /> </div> </form> </body> </html> When you open the page in Listing 12.4, the contents of the EmptyDataText property display. If you need to display more complicated content when no results are returned, such as ASP.NET controls, you can specify an EmptyDataTemplate. The page in Listing 12.5 illus- trates how you can use the EmptyDataTemplate to display complicated HTML content (see Figure 12.3). CHAPTER 12 Using the DetailsView and FormView Controls From the Library of Wow! eBook ptg 575 Using the DetailsView Control 12 LISTING 12.5 ShowEmptyDataTemplate.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <style type=”text/css”> .noMatch { background-color:#ffff66; padding:10px; font-family:Arial,Sans-Serif; } .noMatch h1 { color:red; font-size:16px; font-weight:bold; } </style> <title>Show Empty Data Template</title> FIGURE 12.3 Displaying content when no results are returned. From the Library of Wow! eBook ptg 576 </head> <body> <form id=”form1” runat=”server”> <div> <asp:DetailsView id=”dtlMovies” DataSourceID=”srcMovies” Runat=”server”> <EmptyDataTemplate> <div class=”noMatch”> <h1>No Matching Results!</h1> Please select a different record. </div> </EmptyDataTemplate> </asp:DetailsView> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies WHERE Id=-1” Runat=”server” /> </div> </form> </body> </html> Paging through Data with the DetailsView Control You can use the DetailsView to page through a set of database records by enabling the DetailsView control’s AllowPaging property. The page in Listing 12.6 illustrates how you can page through the records in the Movies database table (see Figure 12.4). CHAPTER 12 Using the DetailsView and FormView Controls From the Library of Wow! eBook ptg 577 Using the DetailsView Control 12 LISTING 12.6 ShowPaging.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Show Paging</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:DetailsView id=”dtlMovies” DataSourceID=”srcMovies” AllowPaging=”true” Runat=”server” /> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” FIGURE 12.4 Paging through records with the DetailsView control. From the Library of Wow! eBook ptg 578 SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies” Runat=”server” /> </div> </form> </body> </html> WARNING In this section, you learn how to take advantage of user interface paging when paging through records with the DetailsView control. Although user interface paging is con- venient, it is not efficient. When working with large sets of records, you should use data source paging. This option is described in Chapter 18, “Using the ObjectDataSource Control.” Paging with AJAX By default, when you page through records with the DetailsView control, the page is posted back to the server each and every time you click a page number. As an alternative, you can take advantage of AJAX to page through records. When you take advantage of AJAX, only the DetailsView control and not the entire page is updated when you navi- gate to a new page of records. NOTE Ajax (Asynchronous JavaScript and XML) enables you to retrieve content from a web server without reloading the page. Ajax works with all modern browsers including Microsoft Internet Explorer, Firefox, Safari, Chrome, and Opera. The page in Listing 12.7 illustrates how you can use AJAX with the DetailsView control. LISTING 12.7 ShowAJAX.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Show Paging</title> </head> <body> <form id=”form1” runat=”server”> <div> CHAPTER 12 Using the DetailsView and FormView Controls From the Library of Wow! eBook ptg 579 Using the DetailsView Control 12 <asp:ScriptManager id=”sm1” runat=”server” /> <%= DateTime.Now %> <asp:UpdatePanel id=”up1” runat=”Server”> <ContentTemplate> <asp:DetailsView id=”dtlMovies” DataSourceID=”srcMovies” AllowPaging=”true” EnablePagingCallbacks=”true” Runat=”server” /> </ContentTemplate> </asp:UpdatePanel> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies” Runat=”server” /> </div> </form> </body> </html> The DetailsView control in Listing 12.7 is contained inside of an UpdatePanel control. When you page through the records displayed by the DetailsView control, only the content inside the UpdatePanel is updated. Furthermore, the page in Listing 12.7 displays the current time. The time is not updated when you navigate to a new page of records. The time is not updated because the entire page is not updated. When you navigate to a new page, only the contents of the DetailsView are updated. NOTE The DetailsView control has an EnablePagingCallbacks property that also enables Ajax. This is a holdover property from the ASP.NET 2.0 Framework. UpdatePanel is a more flexible method of doing Ajax. From the Library of Wow! eBook ptg 580 Customizing the Paging Interface You can customize the appearance of the paging interface by modifying the PagerSettings property. For example, the DetailsView control in Listing 12.8 displays first, previous, next, and last links instead of page numbers (see Figure 12.5). CHAPTER 12 Using the DetailsView and FormView Controls FIGURE 12.5 Using PagerSettings to customize the paging interface. LISTING 12.8 ShowPagerSettings.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Show Pager Settings</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:DetailsView id=”dtlMovies” DataSourceID=”srcMovies” AllowPaging=”true” Runat=”server”> From the Library of Wow! eBook ptg 581 Using the DetailsView Control 12 <PagerSettings Mode=”NextPreviousFirstLast” FirstPageText=”[First Movie]” LastPageText=”[Last Movie]” NextPageText=”[Next Movie]” PreviousPageText=”[Previous Movie]” /> </asp:DetailsView> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies” Runat=”server” /> </div> </form> </body> </html> The PagerSettings class supports the following properties: . FirstPageImageUrl—Enables you to display an image for the first page link. . FirstPageText—Enables you to specify the text for the first page link. . LastPageImageUrl—Enables you to display an image for the last page link. . LastPageText—Enables you to specify the text for the last page link. . Mode—Enables you to select a display mode for the pager user interface. Possible values are NextPrevious, NextPreviousFirstLast, Numeric, and NumericFirstLast. . NextPageImageUrl—Enables you to specify the text for the next page link. . NextPageText—Enables you to specify the text for the next page link. . PageButtonCount—Enables you to specify the number of page number links to display. . Position—Enables you to specify the position of the paging user interface. Possible values are Bottom, Top, TopAndBottom. . PreviousPageImageUrl—Enables you to display an image for the previous page link. . PreviousPageText—Enables you to specify the text for the previous page link. . Visible—Enables you to hide the paging user interface. If you need to customize the paging interface completely, you can use a template. For example, the page in Listing 12.9 displays a list of page numbers in a drop-down list control (see Figure 12.6). From the Library of Wow! eBook ptg 582 CHAPTER 12 Using the DetailsView and FormView Controls LISTING 12.9 ShowPagerTemplate.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> protected void dtlMovies_DataBound(object sender, EventArgs e) { DropDownList ddlPager = (DropDownList)dtlMovies.BottomPagerRow.Cells[0].FindControl(“ddlPager”); for (int i = 0; i < dtlMovies.PageCount; i++) { ListItem item = new ListItem( String.Format(“Record {0}”,i+1), ➥ i.ToString()); if (dtlMovies.PageIndex == i) item.Selected = true; ddlPager.Items.Add(item); } } FIGURE 12.6 Using a PagerTemplate to customize the paging interface. From the Library of Wow! eBook ptg 583 Using the DetailsView Control 12 protected void btnPage_Click(object sender, EventArgs e) { DropDownList ddlPager = (DropDownList)dtlMovies.BottomPagerRow.Cells[0].FindControl(“ddlPager”); dtlMovies.PageIndex = Int32.Parse(ddlPager.SelectedValue); } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Show Pager Template</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:DetailsView id=”dtlMovies” DataSourceID=”srcMovies” AllowPaging=”true” OnDataBound=”dtlMovies_DataBound” Runat=”server”> <PagerTemplate> <asp:DropDownList id=”ddlPager” Runat=”server” /> <asp:Button id=”btnPage” Text=”Select” Runat=”server” OnClick=”btnPage_Click” /> </PagerTemplate> </asp:DetailsView> <asp:SqlDataSource id=”srcMovies” ConnectionString=”<%$ ConnectionStrings:Movies %>” SelectCommand=”SELECT Id,Title,Director,InTheaters FROM Movies” Runat=”server” /> </div> </form> </body> </html> After you open the page in Listing 12.9, you can select a record from the DropDownList control and navigate to the record by clicking the Button control. From the Library of Wow! eBook . ptg 5 74 LISTING 12 .4 ShowEmptyDataText.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC -/ /W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>. to specify the position of the paging user interface. Possible values are Bottom, Top, TopAndBottom. . PreviousPageImageUrl—Enables you to display an image for the previous page link. . PreviousPageText—Enables. ShowPagerTemplate.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC -/ /W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> protected

Ngày đăng: 06/07/2014, 18:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan