Using DataSource Controls


You bind a DataBound control to a DataSource control. A DataSource control is used to represent a particular type of data.

The ASP.NET 2.0 Framework includes the following five DataSource controls:

  • SqlDataSource Represents data retrieved from a SQL relational database, including Microsoft SQL Server, Oracle, or DB2.

  • AccessDataSource Represents data retrieved from a Microsoft Access database.

  • ObjectDataSource Represents data retrieved from a business object.

  • XmlDataSource Represents data retrieved from an XML document.

  • SiteMapDataSource Represents data retrieved from a Site Map Provider. A Site Map Provider represents the page and folder structure of a website.

The ASP.NET Framework contains two basic types of DataSource controls. The SqlDataSource, AccessDataSource, and ObjectDataSource controls all derive from the base DataSourceControl class. These controls can be used to represent tabular data. The XmlDataSource and SiteMapDataSource controls, on the other hand, derive from the base HierarchicalDataSourceControl control. These two controls can be used to represent both tabular and hierarchical data.

A DataBound control is associated with a particular data source control through its DataSourceID property. For example, the page in Listing 8.6 contains a GridView control bound to a SqlDataSource control (see Figure 8.5).

Figure 8.5. Using the SqlDataSource control.


Listing 8.6. BoundGridView.aspx

<%@ Page Language="VB" %> <!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  runat="server">     <title>Bound GridView</title> </head> <body>     <form  runat="server">     <div>     <asp:GridView                  DataSource         Runat="server" />     <asp:SqlDataSource                  ConnectionString="Data Source=.\SQLExpress;             AttachDbFilename=|DataDirectory|MyDatabase.mdf;             Integrated Security=True;User Instance=True"         SelectCommand="SELECT * FROM Movies"         Runat="server" />     </div>     </form> </body> </html>

Using ASP.NET Parameters with DataSource Controls

Many of the DataSource controls support ASP.NET parameters. You use ASP.NET parameters to modify the commands that a DataSource control executes.

Different types of DataSource controls use ASP.NET parameters to represent different types of things. When you use ASP.NET parameters with a SqlDataSource control, the ASP.NET parameters represent ADO.NET parameters. In other words, they represent parameters used with SQL statements.

When you use parameters with the ObjectDataSource control, the ASP.NET parameters represent method parameters. They represent parameters passed to a particular method of a business object.

The SqlDataSource, AccessDataSource, and ObjectDataSource controls all support the following types of Parameter objects:

  • Parameter Represents an arbitrary static value.

  • ControlParameter Represents the value of a control or page property.

  • CookieParameter Represents the value of a browser cookie.

  • FormParameter Represents the value of an HTML form field.

  • ProfileParameter Represents the value of a Profile property.

  • QueryStringParameter Represents the value of a query string field.

  • SessionParameter Represents the value of an item stored in Session state.

For example, the page in Listing 8.7 contains a DropDownList, GridView, and SqlDataSource control. The DropDownList displays a list of movie categories. When you select a new category, the GridView displays matching movies (see Figure 8.6).

Figure 8.6. Using the ControlParameter object.


Listing 8.7. ShowControlParameter.aspx

<%@ Page Language="VB" %> <!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  runat="server">     <title>Show Control Parameter</title> </head> <body>     <form  runat="server">     <div>     <asp:DropDownList                  DataSource         DataTextField="Name"         DataValueField="Id"         Runat="server" />     <asp:Button                  Text="Select"         ToolTip="Select Movie"         Runat="server" />     <hr />     <asp:GridView                  DataSource         Runat="server" />     <asp:SqlDataSource                  ConnectionString="Data Source=.\SQLExpress;             AttachDbFilename=|DataDirectory|MyDatabase.mdf;             Integrated Security=True;User Instance=True"         SelectCommand="SELECT Id,Name FROM MovieCategories"         Runat="server" />     <asp:SqlDataSource                  ConnectionString="Data Source=.\SQLExpress;             AttachDbFilename=|DataDirectory|MyDatabase.mdf;             Integrated Security=True;User Instance=True"         SelectCommand="SELECT Title,Director FROM Movies             WHERE CategoryId=@Id"         Runat="server">         <SelectParameters>             <asp:ControlParameter                 Name="Id"                 Type="int32"                 Control />         </SelectParameters>     </asp:SqlDataSource>     </div>     </form> </body> </html>

Notice that the SqlDataSource control includes a ControlParameter object. The ControlParameter represents the selected item in the DropDownList control. The value of the ControlParameter is used in the SqlDataSource control's SelectCommand to select movies that match the category selected in the DropDownList control.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net