Overview of Data Binding


Data binding refers to the process of dynamically assigning a value to a property of a control at runtime. For example, you can use data binding to bind the properties of a control to a data source such as the contents of a SQL Server database table.

It's important to emphasize that even though this chapter appears in the "Working with ADO.NET" part of this book, the topic of data binding is not limited to working with database data. You can bind the properties of a control to expressions, properties, methods , collections, or even the properties of another control.

The distinctive aspect of data binding is not the data part, but the binding part. Data binding enables you to control exactly when a value is bound to a property.

The best way to get an idea of how data binding works is to look at some examples. The page in Listing 10.1 binds the value of a function to the Text property of a Label control.

Listing 10.1 SimpleBind.aspx
 <Script Runat="Server"> Sub Page_Load   lblMessage.DataBind() End Sub Function GetTime() As String   Return DateTime.Now.ToString( "T" ) End Function </Script> <html> <head><title>SimpleBind.aspx</title></head> <body> <form Runat="Server"> <asp:Label   ID="lblMessage"   Text='<%# GetTime() %>'   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Notice how a value is assigned to the Text property of the Label control:

 
 Text='<%# getTime %>' 

The special <%# and %> tags delimit the data binding expression. In this case, the value of the data binding expression is the value of the GetTime() function.

NOTE

You wrap the value of a data binding expression with apostrophe characters rather than the usual quotation marks. Getting in the habit of always using apostrophes is a good idea because quotation marks can cause errors. Using quotation marks generates an error when the data binding expression itself contains quotation marks.


The Page_Load subroutine in Listing 10.1 looks like this:

 
 Sub Page_Load   lblMessage.DataBind() End Sub 

The DataBind() method is called on the Label control named lblMessage . When the DataBind() method is called, the value of the GetTime() function is assigned to the Text property of the Label control.

The important feature of data binding is that you can control when a data binding expression is evaluated. If the DataBind() method is never called in the page, the lblMessage Text property is never assigned a value.

Every control has a DataBind() method. It is a method of the base Control class used by both the Web and HTML controls. When you call the DataBind() method on a control, all the data binding expressions associated with that control are evaluated. Data binding occurs for both the control itself and all the child controls of the control.

Remember that an ASP.NET page is also a control. If you call DataBind() without specifying a control, the Page control's DataBind() method is called, and all the data binding expressions in the page are evaluated.

The page in Listing 10.2, for example, contains two Label controls that are bound to the GetTime() function (see Figure 10.1).

Listing 10.2 SimplePageDataBind.as px
 <Script Runat="Server"> Sub Page_Load   DataBind() End Sub Function GetTime() As String   Return DateTime.Now.ToString( "T" ) End Function </Script> <html> <head><title>SimplePageDataBind.aspx</title></head> <body> <form Runat="Server"> <asp:Label   ID="lblMessage1"   Text='<%# GetTime() %>'   Runat="Server" /> <p> <asp:Label   ID="lblMessage2"   Text='<%# GetTime() %>'   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Figure 10.1. Calling DataBind() for a page.

graphics/10fig01.jpg

When the Page_Load subroutine executes in Listing 10.2, the DataBind() method is called. When this method is called, the data binding expressions in both Label controls are evaluated. Both Label controls are assigned the current time from the GetTime() function.

Now, look at two quick examples to test your understanding of data binding. The page in Listing 10.3 contains a function, named GetNext() , which increments a number and returns it. The page also contains two Label controls named lblMessage1 and lblMessage2 , which are bound to the function. Look closely at the calls to the DataBind() method and guess what numbers are assigned to each Label control.

Listing 10.3 DataBindTest1.aspx
 <Script Runat="Server"> Dim intNextNum As Integer Sub Page_Load   lblMessage2.DataBind()   lblMessage1.DataBind() End Sub Function GetNext() As String   intNextNum += 1   Return intNextNum End Function </Script> <html> <head><title>DataBindTest1.aspx</title></head> <body> <form Runat="Server"> <asp:Label   ID="lblMessage1"   Text='<%# getNext %>'   Runat="Server" /> <p> <asp:Label   ID="lblMessage2"   Text='<%# getNext %>'   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Notice that the DataBind() method is called on the lblMessage2 control before the method is called on the lblMessage1 control in the Page_Load subroutine. Therefore, the value 2 is assigned to the first Label control, and the value 1 is assigned to the second Label control (see Figure 10.2).

Figure 10.2. The output of DataBindTest1.aspx .

graphics/10fig02.jpg

Here's one last test. Look at the page in Listing 10.4 and guess what values are assigned to the two Label controls.

Listing 10.4 DataBindTest2.aspx
 <Script Runat="Server"> Dim intNextNum As Integer Sub Page_Load   lblMessage1.DataBind()   lblMessage2.DataBind()   DataBind() End Sub Function GetNext() As String   intNextNum += 1   Return intNextNum End Function </Script> <html> <head><title>DataBindTest2.aspx</title></head> <body> <form Runat="Server"> <asp:Label   ID="lblMessage1"   Text='<%# GetNext() %>'   Runat="Server" /> <p> <asp:Label   ID="lblMessage2"   Text='<%# GetNext() %>'   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

In Listing 10.4, the DataBind() method is called three times:

 
 myLabel1.DataBind() myLabel2.DataBind() DataBind() 

When the first DataBind() method is called, the first Label control is assigned the value 1 . When the second DataBind() method is called, the second Label control is assigned the value 2 .

Here's the interesting part: When the DataBind() method is called a third time, data binding occurs for all the controls on the page all over again. So, when all is said and done and the page is displayed, the first Label control displays the value 3 , and the second Label control displays the value 4 (see Figure 10.3).

Figure 10.3. The output of DataBindTest2.aspx .

graphics/10fig03.jpg



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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