![]() ![]() | ||
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.
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 DataSet11.Clear() OleDbDataAdapter1.Fill(DataSet11) TextBox1.DataBind() TextBox2.DataBind() ListBox1.DataBind() DropDownList1.DataBind() CheckBox1.DataBind() RadioButton1.DataBind() CheckBoxList1.DataBind() Label1.DataBind() HyperLink1.DataBind() 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"> <HTML> <HEAD> <title></title> <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 content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <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:TextBox> <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:// www.microsoft.com"></asp:HyperLink> </form> </body> </HTML>
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?
![]() ![]() | ||