Gridview selectedindex changed not firing on first click

匆匆过客 提交于 2019-12-23 03:11:07

问题


Here is my code:

Protected Sub BookingsGV_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BookingsGV.SelectedIndexChanged
    BookingID = BookingsGV.SelectedValue
    Dim query = From a In db.Approvers Where a.ApprovalStatus = False And a.BookingID = BookingID
    Select a.ApproverEmail()
    ApproverList.DataSource = query
    ApproverList.DataBind()
    EmailStatusLabel.Text = String.Empty
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim query = From b In db.Bookings Where b.Approved = False And (From a In db.Approvers Where a.ApprovalStatus = False Select a.BookingID).Contains(b.BookingID) Select b.BookingID, b.DateRequired Distinct Order By DateRequired
    BookingsGV.DataSource = query
    BookingsGV.DataBind()
    If ApproverList.Items.Count > 0 Then
        DetailsPanel.Visible = True
    Else
        DetailsPanel.Visible = False
    End If
End Sub

When I click on a row for the first time it is selected but nothing fires and my details panel is not displayed. When I click for the second time I get the expected results. Please can you tell me how to get around this problem?


回答1:


It's because you are rebinding on postback. Wrap the code from you Page_Load method as follows:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack
        Dim query = From b In db.Bookings Where b.Approved = False And (From a In db.Approvers Where a.ApprovalStatus = False Select a.BookingID).Contains(b.BookingID) Select b.BookingID, b.DateRequired Distinct Order By DateRequired
        BookingsGV.DataSource = query
        BookingsGV.DataBind()
        If ApproverList.Items.Count > 0 Then
            DetailsPanel.Visible = True
        Else
            DetailsPanel.Visible = False
        End If
    End If
End Sub

You might need to move the If statement where you change the DetailsPanel visibility out of the PostBack If statement. It's hard to know exactly what you want to do.



来源:https://stackoverflow.com/questions/5513185/gridview-selectedindex-changed-not-firing-on-first-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!