Binding to Properties

only for RuBoard

Data binding to properties is slightly different than binding to traditional data sources. In the previous examples, you've been binding a DataGrid or similar server control to a data source that exposes data values in grid-like structures. Properties are different because they typically consist of a single value. You can't bind a DataGrid to a property because the property doesn't contain a traditional row/column structure. However, you can bind a server control, such as a Label control, to a property. The single value of the property can be rendered as the Label.Text property.

This type of data binding requires a slightly different syntax. You don't have to set a DataSource property. Rather, you bind a property of the server control to the property value you wish to render. This type of data binding uses the <%# and %> tags to delimit the binding expression. Alternatively, you can set the server control's property declaratively . Listing 5.7 shows an ASP.NET Web Form that binds two Label server controls to the Page.IsPostBack property (a Boolean value).

Listing 5.7 Data Binding Server Controls to Properties
 [VB] 01: <%@ Page Language="VB" %> 02: 03: <script runat="server"> 04:   Sub Page_Load(Source As Object, E As EventArgs) 05:     Label1.Text = Page.IsPostBack 06:     Page.DataBind() 07:   End Sub 08: </script> [C#] 01: <%@ Page Language="C#" %> 02: 03: <script runat="server"> 04:   void Page_Load(Object sender, EventArgs e){ 05:     Label1.Text = Page.IsPostBack.ToString(); 06:     Page.DataBind(); 07:   } 08: </script> [VB & C#] 09: <html> 10: <head> 11: <title>Programming Datadriven Web Applications with ASP.NET - Chapter 5</title> 12: </head> 13: <body> 14: </head> 15: <body> 16:   <p>Label 1: <asp:Label id="Label1" runat="server" /></p> 17:   <p>Label 2: <asp:Label id="Label2" Text='<%# Page.IsPostBack %>' runat="server" /></ graphics/ccc.gif p> 18: </body> 19: </html> 

In Listing 5.7, you create an ASP.NET Web Form with two Label server controls. Each control displays the same Page.IsPostBack property value. This is done with two different approaches. On line 5, you set the Text property of the first Label control to the Page.IsPostBack property value. The Text value is being set declaratively, and will render correctly without calling DataBind() . For the other control, you use the <%#...%> data binding expression. This is done on line 17 with the expression <%# Page.IsPostBack %> . Figure 5.6 shows the rendered Web page.

Figure 5.6. Server controls can be bound to properties of .NET objects, such as the Page.IsPostBack property.
graphics/05fig06.gif

Note

In Listing 5.7, you used the ToString() method only in the C# example. Visual Basic.Net can implicitly convert a Boolean value to a String data type, whereas in C# this must be done explicitly.


only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

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