![]() ![]() | ||
As you know, datasets do not maintain a "current record" that is displayed in bound controls. In Windows forms, that's handled with a BindingContext object, which lets you set the record bound controls display. But there is no BindingContext object available in Web forms. So how do you bind a particular record to the controls in a Web form?
To see how this works, take a look at the WebDataNavigation example on the CD-ROM. This example, shown in Figure 23.6, lets the user move through the records in a database with navigation buttons, displaying selected fields of those records. (This is not to say it's a good idea to use navigation buttons in Web data applications, because each time you click one, a server round trip is needed. This example is just to show you how to select the record that controls bind to.)
To single out a particular record to display in bound controls, I'm going to use not a dataset but a data view (see "Using Data Views" in Chapter 20). You can use the RowFilter property of a data view to select the record(s) to work with; I'll use that property to select the record to display in the bound controls, and bind those controls to the data view, not the dataset.
To follow along in this example, create a Web application now, drag a data adapter to the main form in that application, connect the data adapter to the authors table in the pubs database, and create a dataset, DataSet11, using that data adapter. In this example, we'll use a data view, so click the Data tab in the toolbox and drag a DataView object onto the main form. Next, set the Table property of the data view to DataSet11.authors.
Then add two text boxes, as you see in Figure 23.6, to display authors' first and last names. Bind them to the au_fname and au_lname fields using the data view, DataView1 (not the dataset) and add this code to the Page_Load event:
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() End Sub
That gets us started displaying data from the first record in the authors table. The next step is to display the other records as well when the user clicks the navigation buttons.
To keep our place in the dataset, we'll need an index value of some kind, so I'll create a new variable named Index, and save it across server round trips using the ViewState property (see "Saving Program Data across Server Round Trips" in Chapter 14). For example, when the user clicks the << button, we want to move to the beginning of the dataset, so I'll set Index to 0 for the first record:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim ID As String Dim Index As Integer Index = 0 Me.ViewState("Index") = Index ⋮
Now we have the numeric index of the record we want the bound text boxes to display, but how do we make them display that record? To select a record in a data view, I'll use the RowFilter property. You set this property to a string specifying the text value of a field, like "au_id = '222-33-555'", where I'm selecting the record whose au_id field is "222-33-555". That's easy enough to implement in code; here, I'm finding the author ID of the record corresponding to the value in Index:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim ID As String Dim Index As Integer Index = 0 Me.ViewState("Index") = Index ID = DataSet11.Tables(0).Rows(Index).Item("au_id") ⋮
Then, I use that ID value with the RowFilter property and bind the text boxes to the newly selected record, like this:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim ID As String Dim Index As Integer Index = 0 Me.ViewState("Index") = Index ID = DataSet11.Tables(0).Rows(Index).Item("au_id") DataView1.RowFilter = "au_id = "" & ID & "'" TextBox1.DataBind() TextBox2.DataBind() End Sub
That's all it takes—now the user can click the << button and move to the first record in the dataset. Because the text boxes are bound to the data view, they'll be updated with the new record's data. Here's how to implement the <, >, and >> buttons in similar fashion:
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim ID As String Dim Index As Integer Index = Me.ViewState("Index") Index -= 1 If Index < 0 Then Index = 0 End If Me.ViewState("Index") = Index ID = DataSet11.Tables(0).Rows(Index).Item("au_id") DataView1.RowFilter = "au_id = '" & ID & "'" TextBox1.DataBind() TextBox2.DataBind() End Sub Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click Dim ID As String Dim Index As Integer Index = Me.ViewState("Index") Index += 1 If Index > DataSet11.Tables(0).Rows.Count - 1 Then Index = DataSet11.Tables(0).Rows.Count - 1 End If Me.ViewState("Index") = Index ID = DataSet11.Tables(0).Rows(Index).Item("au_id") DataView1.RowFilter = "au_id = '" & ID & "'" TextBox1.DataBind() TextBox2.DataBind() End Sub Private Sub Button4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button4.Click Dim ID As String Dim Index As Integer Index = DataSet11.Tables(0).Rows.Count - 1 Me.ViewState("Index") = Index ID = DataSet11.Tables(0).Rows(Index).Item("au_id") DataView1.RowFilter = "au_id = '" & ID & "'" TextBox1.DataBind() TextBox2.DataBind() End Sub
And we're set—that's all it takes. Now you can select which record the controls in a Web form are bound to.
![]() ![]() | ||