Understanding Templates and DataBinding Expressions


Almost all the DataBound controls support templates. You can use a template to format the layout and appearance of each of the data items that a DataBound control displays. Within a template, you can use a DataBinding expression to display the value of a data item.

In this section, you learn about the different kinds of templates and DataBinding expressions that you can use with the DataBound controls.

Using Templates

Every DataBound control included in the ASP.NET 2.0 Framework supports templates with the sole exception of the treeView control. The Repeater, DataList, and FormView controls all require you to use templates. If you don't supply a template, then these controls display nothing. The GridView, DetailsView, and Menu controls also support templates, but they do not require a template.

For example, when you use the Repeater control you must supply an ItemTemplate. The Repeater control uses the ItemTemplate to format each of the records that it displays. Listing 8.9 contains a Repeater control that formats each of the records from the Movies database table (see Figure 8.8).

Figure 8.8. Using an ItemTemplate.


Listing 8.9. ShowItemTemplate.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 ItemTemplate</title> </head> <body>     <form  runat="server">     <div>     <asp:Repeater                  DataSource         Runat="server">         <ItemTemplate>         <%#Eval("Title")%>         <i>directed by</i>         <%#Eval("Director")%>         <hr />         </ItemTemplate>     </asp:Repeater>     <asp:SqlDataSource                  ConnectionString="Data Source=.\SQLExpress;             AttachDbFilename=|DataDirectory|MyDatabase.mdf;             Integrated Security=True;User Instance=True"         SelectCommand="SELECT Title,Director FROM Movies"         Runat="server" />     </div>     </form> </body> </html>

A template can contain HTML, DataBinding expressions, and other controls. In Listing 8.9, the template includes the following two DataBinding expressions:

<%# Eval("Title") %> <%# Eval("Director") %>


The first DataBinding expression displays the value of the Title column and the second DataBinding expression displays the value of the Director column.

A template can contain other controlseven other DataBound controls. For example, the page in Listing 8.10 displays a list of hyperlinks (see Figure 8.9).

Listing 8.10. ShowLinks.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 Links</title> </head> <body>     <form  runat="server">     <div>     <asp:Repeater                  DataSource         Runat="server">         <ItemTemplate>         <asp:HyperLink                          Text='<%# Eval("Title") %>'             NavigateUrl='<%# Eval("Id", "Details.aspx?id={0}") %>'             runat="server" />         <br />         </ItemTemplate>     </asp:Repeater>     <asp:SqlDataSource                  ConnectionString="Data Source=.\SQLExpress;             AttachDbFilename=|DataDirectory|MyDatabase.mdf;             Integrated Security=True;User Instance=True"         SelectCommand="SELECT Id, Title FROM Movies"         Runat="server" />     </div>     </form> </body> </html>

Figure 8.9. Displaying a list of hyperlinks.


In Listing 8.10, a HyperLink control is displayed for each item from the data source. The HyperLink control displays the movie title and links to a details page for the movie.

Using DataBinding Expressions

A DataBinding expression is a special type of expression that is not evaluated until runtime. You mark a databinding expression in a page by wrapping the expression in opening <%# and closing %> brackets.

A DataBinding expression isn't evaluated until a control's DataBinding event is raised. When you bind a DataBound control to a DataSource control declaratively, this event is raised automatically. When you bind a DataSource control to a data source programmatically, the DataBinding event is raised when you call the DataBind() method.

For example, the page in Listing 8.11 contains a DataList control that contains a template that includes two DataBinding expressions.

Listing 8.11. ShowDataList.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 DataList</title> </head> <body>     <form  runat="server">     <div>     <asp:DataList                  DataSource         Runat="server">         <ItemTemplate>         <b>Movie Title:</b>         <%#Eval("Title")%>         <br />         <b>Date Released:</b>         <%#Eval("DateReleased", "{0:D}") %>         <hr />         </ItemTemplate>     </asp:DataList>     <asp:SqlDataSource                  ConnectionString="Data Source=.\SQLExpress;             AttachDbFilename=|DataDirectory|MyDatabase.mdf;             Integrated Security=True;User Instance=True"         SelectCommand="SELECT Title,Director,DateReleased FROM Movies"         Runat="server" />     </div>     </form> </body> </html>

The first DataBinding expression displays the title of the movie and the second DataBinding expression displays the date the movie was released (see Figure 8.10).

Figure 8.10. Using databinding expressions.


Both DataBinding expressions call the Eval() method. The Eval() method is a protected method of the Page class. Behind the scenes, the Page.Eval() method calls the shared (static) DataBinder.Eval() method. If you want to be verbose, instead of using the Eval() method, you could use the following two expressions:

<%# DataBinder.Eval(Container.DataItem, "Title") %> <%# DataBinder.Eval(Container.DataItem, "DateReleased", "{0:D}" ) %>


In ASP.NET version 1.x, you had to use DataBinder.Eval() when displaying data items in a template. However, Microsoft took pity on programmers in ASP.NET 2.0 and provided us with the shorter syntax.

Note

Technically, the Eval() method uses reflection when evaluating the data item to find a property with a certain name. You do pay a performance penalty when you use reflection.

As an alternative, you can improve the performance of your DataBinding expressions by casting the data items to a particular type like this:

<%# CType(Container.DataItem, System.Data.DataRowView)("Title")%>



Notice that the second DataBinding expression in Listing 8.11 includes a second parameter. The Eval() method, optionally, accepts a format string. You can use the format string to format values such as dates and currency amounts. In Listing 8.11, the format string is used to format the DateReleased column as a long date.

Note

Format strings use format specifiers such as the D format specifier when formatting strings. You can find a list of format specifiers by looking up Formatting Types in the index of the Microsoft .NET Framework SDK 2.0 documentation.


You can call other methods than the Eval() method in a DataBinding expression. For example, the DataBinding expression in Listing 8.12 calls a method named FormatTitle() to format the movie titles.

Listing 8.12. FormatMovieTitles.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Public Function FormatTitle(ByVal title As Object) As String         Return "<b>" + title.ToString().ToUpper() + "</b>"     End Function </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Format Movie Titles</title> </head> <body>     <form  runat="server">     <div>     <asp:Repeater                  DataSource         Runat="server">         <ItemTemplate>         <%# FormatTitle(Eval("Title")) %>         <hr />         </ItemTemplate>     </asp:Repeater>     <asp:SqlDataSource                  ConnectionString="Data Source=.\SQLExpress;             AttachDbFilename=|DataDirectory|MyDatabase.mdf;             Integrated Security=True;User Instance=True"         SelectCommand="SELECT Title FROM Movies"         Runat="server" />     </div>     </form> </body> </html>

The FormatTitle() method is defined in the page in Listing 8.12. This method formats each of the titles displayed by the Repeater control by making each title bold and uppercase (see Figure 8.11).

Figure 8.11. Formatting movie titles.


Using Two-Way DataBinding Expressions

The ASP.NET 2.0 Framework actually supports two types of templates and two types of DataBinding expressions. The ASP.NET Framework supports both one-way DataBinding expressions and two-way DataBinding expressions.

Up to this point, we have used one-way DataBinding expressions exclusively. In a one-way DataBinding expression, you use the DataBinding expression to display the value of a data item. You use the Eval() method to display the value of a one-way DataBinding expression.

In a two-way DataBinding expression, you not only can display the value of a data item, you also can modify the value of a data item. You use the Bind() method when working with a two-way DataBinding expression.

For example, the page in Listing 8.13 contains a FormView control that includes a template for editing a movie record in the Movies database table (see Figure 8.12).

Figure 8.12. Editing a movie.


Listing 8.13. ShowFormView.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 FormView</title> </head> <body>     <form  runat="server">     <div>     <asp:FormView                  DataKeyNames="Id"         DataSource         DefaultMode="Edit"         AllowPaging="true"         Runat="server">         <EditItemTemplate>         <asp:Label                          Text="Title:"             AssociatedControl             Runat="server" />         <asp:TextBox                          Text='<%#Bind("Title")%>'             Runat="server" />         <br />         <asp:Label                          Text="Director:"             AssociatedControl             Runat="server" />         <asp:TextBox                          Text='<%#Bind("Director")%>'             Runat="server" />         <br />         <asp:Button                          Text="Update"             CommandName="Update"             Runat="server" />         </EditItemTemplate>     </asp:FormView>     <asp:SqlDataSource                  ConnectionString="Data Source=.\SQLExpress;             AttachDbFilename=|DataDirectory|MyDatabase.mdf;             Integrated Security=True;User Instance=True"         SelectCommand="SELECT Id, Title,Director,DateReleased FROM Movies"         UpdateCommand="UPDATE Movies SET Title=@Title,             Director=@Director WHERE Id=@Id"         Runat="server" />     </div>     </form> </body> </html>

Notice that the FormView contains an EditItemTemplate. The EditItemTemplate contains three TextBox controls. Each TextBox control has a two-way DataBinding expression assigned to its Text property.

The DataBinding expressions associate the TextBox control properties with the properties of the data item being edited. When you click the Update button, any changes you make to the Text properties are updated in the Movies database table.

Note

Templates that support one-way databinding implement the ITemplate interface, and templates that support two-way databinding implement the IBindableTemplate interface.





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