问题
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