JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Binding Standard Web Server Controls

You can see a number of common Web server controls bound to a data source in the WebDataBinding example on the CD-ROM, shown at work in Figure 23.5.

Click To expand
Figure 23.5: The WebDataBinding example.

The controls at left in Figure 23.5 are all simple-bound controls. I've bound the Text property of the text boxes, the label, and the hyperlink, and the Checked property of the radio button and checkbox. As discussed, you implement simple binding with the (DataBindings) property, and I've done that here.

The controls on the right are complex-bound controls. At top, you can see a list box. I've set the DataSource property for this list box to the dataset in this example, and the DataTextField property to the au_lname field to display the authors' last names, as you see in Figure 23.5. Underneath the list box is a drop-down list, which also lists all the authors' last names. That's bound in the same way as the list box. And under the drop-down list you can see a bound checkbox list, also bound the same way. At run time, the checkbox list displays a checkbox for each record it's bound to, as you see in Figure 23.5. (Radio button lists act the same way.)

In addition, I use the DataBind method for each control when the page loads, like this:

Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

The actual data binding takes place in the WebForm1.aspx file, using an object of the DataBinder class. You don't have to use this class yourself—Visual Basic handles all the details—but it's instructive to see how this works:

<%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="WebForm1.aspx.vb" Inherits="WebDataBinding.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema
  <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server">
<asp:TextBox id=TextBox1 style="Z-INDEX: 101; LEFT: 100px; POSITION:
absolute; TOP: 41px" runat="server" Text=
'<%# DataBinder.Eval(DataSet11,
"Tables[authors].DefaultView.[0].au_fname") %>'>
<asp:CheckBoxList id=CheckBoxList1 style="Z-INDEX: 107; LEFT: 285px;
POSITION: absolute; TOP: 165px" runat="server" DataTextField="au_lname"
DataSource="<%# DataSet11 %>"></asp:CheckBoxList>
<asp:TextBox id=TextBox2 style="Z-INDEX: 102; LEFT: 101px; POSITION:
absolute; TOP: 79px" runat="server" Text=
'<%# DataBinder.Eval(DataSet11,
"Tables[authors].DefaultView.[0].au_lname") %>'></asp:TextBox>
<asp:ListBox id=ListBox1 style="Z-INDEX: 103; LEFT: 291px; POSITION:
absolute; TOP: 39px" runat="server" Height="70px" Width="156px"
DataSource="<%# DataSet11 %>" DataTextField="au_lname"></asp:ListBox>
<asp:DropDownList id=DropDownList1 style="Z-INDEX: 104; LEFT: 292px;
POSITION: absolute; TOP: 129px" runat="server" Height="22px" Width="156px"
DataTextField="au_lname" DataSource="<%# DataSet11 %>"></asp:DropDownList>
<asp:CheckBox id=CheckBox1 style="Z-INDEX: 105; LEFT: 97px; POSITION:
absolute; TOP: 173px" runat="server" Text="Contract" Checked=
'<%# DataBinder.Eval(DataSet11,
"Tables[authors].DefaultView.[0].contract") %>'></asp:CheckBox>
<asp:RadioButton id=RadioButton1 style="Z-INDEX: 106; LEFT: 96px;
POSITION: absolute; TOP: 144px" runat="server" Text="Contract"
Checked='<%# DataBinder.Eval(DataSet11,
"Tables[authors].DefaultView.[0].contract") %>'></asp:RadioButton>
<asp:Label id=Label1 style="Z-INDEX: 108; LEFT: 102px; POSITION:
absolute; TOP: 114px" runat="server" Text=
'<%# DataBinder.Eval(DataSet11,
"Tables[authors].DefaultView.[0].au_lname") %>'></asp:Label>
<asp:HyperLink id=HyperLink1 style="Z-INDEX: 109; LEFT: 100px; POSITION:
absolute; TOP: 203px" runat="server"
Text='<%# DataBinder.Eval(DataSet11,
"Tables[authors].DefaultView.[0].au_lname") %>' NavigateUrl="http://"></asp:HyperLink>

Note, however, that the simple-bound controls in Figure 23.5 only bind to the first record in the dataset. What if you want to display the data from other records as well?

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor