1

Within a vb asp.net webform I have a select case statement within a gridview databound procedure which is based on the values from a dropdown list as stored in a variable.

Protected Sub gvProgressGrid_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProgressGrid.DataBound
    Dim strYear As String = DdlYear.Text
     Select Case strYear
        Case 11
            gvProgressGrid.Visible = True
        Case 10
            gvProgressGrid.Visible = True
        Case 9
            gvProgressGrid.Visible = True
        Case 8
            gvProgressGrid.Visible = False
        Case 7
            gvProgressGrid.Visible = False
    End Select
End Sub

As default the gridview displays and when I select 7 or 8 from the dropdown the gridview disappears ok. However, once disappeared if I select 9-11 then the grid doesn't reappear.

DOK
  • 32,337
  • 7
  • 60
  • 92
Matt
  • 263
  • 2
  • 10
  • 35

1 Answers1

0

Rather than use the Databound event for your Gridview, I would use the SelectedIndexChanged event of your DropDownList to house this logic:

Protected Sub DdlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DdlYear.SelectedIndexChanged
    Dim strYear As String = DdlYear.Text
    Select Case strYear
        Case 11
            gvProgressGrid.Visible = True
        Case 10
            gvProgressGrid.Visible = True
        Case 9
            gvProgressGrid.Visible = True
        Case 8
            gvProgressGrid.Visible = False
        Case 7
            gvProgressGrid.Visible = False
    End Select
End Sub

This makes more sense, semantically, since the result is dependent on the value of of your dropdown (DdlYear.Text).

It also will probably function more correctly, as I imagine this problem has to do with how your GridView is databound (it's probably only databound on first page load, not on postbacks).

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66