JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Navigating in Datasets

How do you navigate through the data in a dataset or select the current record bound to controls in a Web form? Datasets don't maintain a current record to bind to data controls, and bound controls in Web applications don't maintain an active connection to a dataset. However, one way of letting the user select which record should be displayed in bound controls is to create a data view, as we did in the In Depth section of this chapter, bind those controls to the data view, and use the RowFilter property of the data view to select the record you want the bound controls to display.

We did that in the WebDataNavigation example on the CD-ROM, as discussed in the In Depth section of this chapter. That example displayed some fields of the authors table in the pubs database in bound text boxes, and let the user select which record to look at with navigation buttons. To keep track of which record we're currently displaying, I set up a variable named Index and persisted it with the ViewState property. And to display the record with a specific numeric index, I used the RowFilter property of a data view object, and bound the text boxes to that data view. Here's the important code from this example—the navigation buttons—which set the RowFilter property as needed:

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

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

You can see the results in Figure 23.6. For more details, see the In Depth section of this chapter.

Related solution:

Found on page:

Saving Program Data across Server Round Trips

654

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor