9.4 Binding to Other Simple Controls

The simplest binding is to bind one control (for example, a label) to another control (e.g., a list box). As a demonstration of data-binding one control to a second, add a new label to the form, and bind its contents to the drop-down selection:

<tr>    <td>       Chosen Book Title    </td>    <td>       <asp:Label        Text="<%# ddlBooks.SelectedItem.Text %>"        Runat="server" />    </td> </tr>

This new label control is bound to the text value of the selected item in ddlBooks. The <%# %> tags accomplish the data binding. The only additional change to the code is in WebForm1_Load, where you bind the entire page, rather than a single control:

Page.DataBind(  )

You could, of course, bind each of the controls individually rather than binding the entire page. In any case, the result, shown in Figure 9-3 is that the control is bound to the text of the control.

Figure 9-3. Data binding to a control
figs/pan2_0903.gif


Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 156

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