Using Programmatic DataBinding


When you bind a DataBound control to a DataSource control, you are taking advantage of declarative databinding. When you use declarative databinding, the ASP.NET Framework handles all the messy details of deciding when to retrieve the data items represented by a DataSource control.

In certain situations, you'll want to handle these messy details yourself. For example, you might want to force a GridView control to refresh the data it displays after you add a new record to a database table. Or, you might want to bind a DataBound control to a data source that can't be easily represented by one of the existing DataSource controls. In these situations, you'll want to use programmatic databinding.

Note

The ASP.NET 1.x Framework supported only programmatic databinding. The first version of the Framework did not include any of the DataSource controls.


Every DataBound control has a DataSource property and a DataBind() method. By using this property and method, you can programmatically associate a DataBound control with a data source.

For example, the page in Listing 8.8 displays a list of all the fonts installed on your computer (see Figure 8.7).

Figure 8.7. Programmatic databinding.


Listing 8.8. ShowFonts.aspx

<%@ Page Language="VB" %> <%@ Import Namespace="System.Drawing.Text" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Private Sub Page_Load()         If Not Page.IsPostBack Then             Dim fonts As New InstalledFontCollection()             GridView1.DataSource = fonts.Families             GridView1.DataBind()         End If     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show Fonts</title> </head> <body>     <form  runat="server">     <div>     <asp:GridView                  Runat="server" />     </div>     </form> </body> </html>

Note

The programmatic databinding in Listing 8.8 could have been avoided by taking advantage of the ObjectDataSource control. This DataSource control is discussed in detail in Chapter 15, "Using the ObjectDataSource Control."


The list of fonts is displayed by a GridView control. The actual list of fonts is retrieved from the InstalledFontCollection class (which inhabits the System.Drawing.Text namespace). The list of fonts is assigned to the GridView control's DataSource property, and the DataBind() method is called.

In Listing 8.8, a collection of fonts has been assigned to the DataSource property. In general, you can assign any object that implements the IEnumerable interface to the DataSource property. For example, you can assign collections, arrays, DataSets, DataReaders, DataViews, and enumerations to the DataSource property.

Note

Particular DataBound controls support different data sources. For example, you can assign any object that implements the IEnumerable, IListSource or IDataSource interface to the DataSource property of a GridView control.


When you call the DataBind() method, the GridView control actually retrieves its data from the data source. The control iterates through all of the items represented by the data source and displays each item. If you neglect to call the DataBind() method, the control will never display anything.

Notice that the GridView is bound to its data source only when the page is requested for the first time. The Page.IsPostBack property is used to determine whether or not the page has been posted back to the server. You don't need to rebind the GridView to its data source every time the page is requested because the GridView uses View State to remember the data items that it displays.

You can't mix declarative and programmatic databinding. If you attempt to use both the DataSource and DataSourceID properties, then you will get an exception.

On the other hand, you can call the DataBind() method even when you have declaratively bound a control to a DataSource control. When you explicitly call DataBind(), the DataBound control grabs the data items from its DataSource control again. Explicitly calling DataBind() is useful when you want to refresh the data displayed by a DataBound 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