ASP.NET 4 Unleased - p 87 pptx

10 311 0
ASP.NET 4 Unleased - p 87 pptx

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

Thông tin tài liệu

ptg 834 CHAPTER 18 Using the ObjectDataSource Control LISTING 18.41 ShowMovieDataSource.aspx <%@ Page Language=”C#” %> <%@ Register TagPrefix=”custom” Namespace=”AspNetUnleashed.Samples” %> <!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 Movie DataSource</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:GridView id=”grdMovies” DataSourceID=”srcMovies” Runat=”server” /> <custom:MovieDataSource id=”srcMovies” Runat=”server” /> FIGURE 18.11 Using the MovieDataSource control to display movies. From the Library of Wow! eBook ptg 835 Extending the ObjectDataSource Control 18 </div> </form> </body> </html> The custom control must be registered with a <%@ Register %> directive at the top of Listing 18.41. After you register the control, you can simply declare the MovieDataSource control in the page to represent the contents of the Movies database table. NOTE As an alternative to registering the MovieDataSource control in a page, you can regis- ter the control for an entire application in the web configuration file within the <pages> element. Creating Custom Parameter Objects The standard DataSource Parameter objects included in the ASP.NET Framework enable you to represent objects such as query string values, items from Session state, and the values of control properties. If none of the standard Parameter objects satisfy your require- ments, you always have the option of creating a custom Parameter object. You create a custom Parameter object by deriving a new class from the base Parameter class. In this section, we create two custom parameters. The first is a UsernameParameter that automatically represents the current username. Next is a PagePropertyParameter that represents the current value of a property contained in the page. Creating a Username Parameter The UsernameParameter class is contained in Listing 18.42. The class in Listing 18.42 derives from the Parameter class and overrides the Evaluate() method of the base class. The Evaluate() method determines what the parameter represents. LISTING 18.42 UsernameParameter.cs using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MyControls { public class UsernameParameter : Parameter { protected override object Evaluate(HttpContext context, Control control) { if (context != null) From the Library of Wow! eBook ptg 836 CHAPTER 18 Using the ObjectDataSource Control return context.User.Identity.Name; else return null; } } } The UsernameParameter returns the current username. The parameter retrieves this infor- mation from the current HttpContext passed to the Evaluate() method. The UsernameParameter is used in the page in Listing 18.43. LISTING 18.43 ShowUsernameParameter.aspx <%@ Page Language=”C#” %> <%@ Register TagPrefix=”custom” Namespace=”MyControls” %> <!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”> .guestbook td,.guestbook th { padding:5px; font:14px Arial,Sans-Serif; } </style> <title>Show Username Parameter</title> </head> <body> <form id=”form1” runat=”server”> <div> <asp:FormView id=”frmGuestbook” DataSourceID=”srcGuestbook” DefaultMode=”Insert” Runat=”server”> <InsertItemTemplate> <asp:Label ID=”lblComment” Text=”Comment:” AssociatedControlID=”txtComment” Runat=”server” /> From the Library of Wow! eBook ptg 837 Extending the ObjectDataSource Control 18 <br /> <asp:TextBox id=”txtComment” Text=’<%# Bind(“comment”) %>’ TextMode=”MultiLine” Columns=”50” Rows=”4” Runat=”server” /> <br /> <asp:Button id=”btnInsert” Text=”Add Entry” CommandName=”Insert” Runat=”server” /> </InsertItemTemplate> </asp:FormView> <hr /> <asp:GridView id=”grdGuestbook” DataSourceID=”srcGuestbook” CssClass=”guestbook” Runat=”server” /> <asp:ObjectDataSource id=”srcGuestbook” TypeName=”GuestbookComponent” SelectMethod=”GetEntries” InsertMethod=”AddEntry” Runat=”server”> <InsertParameters> <custom:UsernameParameter name=”username” /> </InsertParameters> </asp:ObjectDataSource> </div> </form> </body> </html> The UsernameParameter is declared in the ObjectDataSource control’s InsertParameters collection. When you add a new entry to the guestbook, your username is added automat- ically (see Figure 18.12). From the Library of Wow! eBook ptg 838 CHAPTER 18 Using the ObjectDataSource Control Creating a Page Property Parameter PagePropertyParameter enables you to represent an arbitrary property of the current page. The property being represented can return whatever type of value you want. The code for the PagePropertyParameter is contained in Listing 18.44. LISTING 18.44 PagePropertyParameter.cs using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MyControls { public class PagePropertyParameter : Parameter { private string _propertyName; protected override object Evaluate(HttpContext context, Control control) { return DataBinder.Eval(control.Page, PropertyName); } FIGURE 18.12 Inserting records with the UsernameParameter. From the Library of Wow! eBook ptg 839 Extending the ObjectDataSource Control 18 public string PropertyName { get { return _propertyName; } set { _propertyName = value; } } } } The component in Listing 18.44 overrides the Evaluate method of the base Parameter class. The DataBinder.Eval() method is used to return the value of a property of the current page. The page in Listing 18.45 uses the PagePropertyParameter to represent a property of the page named CurrentUsername. This property returns the current username. LISTING 18.45 ShowPagePropertyParameter.aspx <%@ Page Language=”C#” %> <%@ Register TagPrefix=”custom” Namespace=”MyControls” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> Public ReadOnly Property CurrentUsername() As String Get Return User.Identity.Name End Get End Property </script> <html xmlns=”http://www.w3.org/1999/xhtml”> <head id=”Head1” runat=”server”> <style type=”text/css”> .guestbook td,.guestbook th { padding:5px; font:14px Arial,Sans-Serif; } </style> <title>Show Page Property Parameter</title> </head> <body> <form id=”form1” runat=”server”> <div> From the Library of Wow! eBook ptg 840 CHAPTER 18 Using the ObjectDataSource Control <asp:FormView id=”frmGuestbook” DataSourceID=”srcGuestbook” DefaultMode=”Insert” Runat=”server”> <InsertItemTemplate> <asp:Label ID=”lblComment” Text=”Comment:” AssociatedControlID=”txtComment” Runat=”server” /> <br /> <asp:TextBox id=”txtComment” Text=’<%# Bind(“comment”) %>’ TextMode=”MultiLine” Columns=”50” Rows=”4” Runat=”server” /> <br /> <asp:Button id=”btnInsert” Text=”Add Entry” CommandName=”Insert” Runat=”server” /> </InsertItemTemplate> </asp:FormView> <hr /> <asp:GridView id=”grdGuestbook” DataSourceID=”srcGuestbook” CssClass=”guestbook” Runat=”server” /> <asp:ObjectDataSource id=”srcGuestbook” TypeName=”GuestbookComponent” SelectMethod=”GetEntries” InsertMethod=”AddEntry” Runat=”server”> <InsertParameters> <custom:PagePropertyParameter Name=”Username” PropertyName=”CurrentUsername” /> From the Library of Wow! eBook ptg 841 Summary 18 </InsertParameters> </asp:ObjectDataSource> </div> </form> </body> </html> In Listing 18.45, the PagePropertyParameter represents the current username. Because the PagePropertyParameter can represent any page property, the parameter could represent any type of value. Summary In this chapter, you learned how to use the ObjectDataSource control to represent differ- ent types of objects. In the first section, you were provided with sample code that demon- strated how you can use the ObjectDataSource control to represent a collection, DataReader, DataSet, a LINQ to SQL query, and a web service. We also discussed how you can use the ObjectDataSource control to page, sort, and filter data. You learned how to implement both user interface paging and data source paging, which enables you to efficiently work with very large sets of records. Next, we examined how you can handle ObjectDataSource control events. You learned how to add and modify the parameters represented by the ObjectDataSource control. You also learned how to gracefully handle errors raised when executing an ObjectDataSource control method. Finally, we discussed two methods of extending the ObjectDataSource control. You learned how to derive a new control from the base ObjectDataSource control to represent specialized data sources such as a Product data source. We also discussed how you can create custom Parameter objects that can be used with the ObjectDataSource control. From the Library of Wow! eBook ptg This page intentionally left blank From the Library of Wow! eBook ptg CHAPTER 19 Building Data Access Components with ADO.NET IN THIS CHAPTER . Connected Data Access . Disconnected Data Access . Executing Asynchronous Database Commands . Building Database Objects with the .NET Framework . Summary In the previous chapter, you learned how to use the ObjectDataSource control to bind data controls—such as the GridView or DetailsView controls—to a data access component. In this chapter, we shift focus from the ObjectDataSource control to the topic of building data access components. This chapter provides you with an overview of ADO.NET, which is the main set of classes included in .NET Framework for working with database data. For example, under the covers, the SqlDataSource control uses ADO.NET classes to retrieve data from a SQL Server database. The classes in ADO.NET Framework support two models of data access: a connected and disconnected model. In the first part, you learn how to take advantage of the connected model of data access. You learn how to use ADO.NET Connection, Command, and DataReader classes to retrieve and modify database data. In the next part, you learn how to take advantage of the disconnected model of data access represented by ADO.NET DataAdapter, DataTable, DataView, and DataSet classes. You can use these classes to build an in-memory representation of database data. Finally, at the end of this chapter, we explore two advanced topics. You learn how to take advantage of two important new features included in ADO.NET 2.0. First, you learn how to improve the performance of your database access code by executing asynchronous database commands. You learn how to build asynchronous ASP.NET pages that execute asynchro- nous ADO.NET commands. From the Library of Wow! eBook . 18 .45 uses the PagePropertyParameter to represent a property of the page named CurrentUsername. This property returns the current username. LISTING 18 .45 ShowPagePropertyParameter.aspx <%@ Page. Listing 18 .45 , the PagePropertyParameter represents the current username. Because the PagePropertyParameter can represent any page property, the parameter could represent any type of value. Summary In. arbitrary property of the current page. The property being represented can return whatever type of value you want. The code for the PagePropertyParameter is contained in Listing 18 .44 . LISTING 18 .44

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

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

Tài liệu liên quan