问题
Gridview Code:
<asp:GridView ID="gvVessel" runat="server" AutoGenerateColumns="false" GridLines="None"
EmptyDataText="No Vessels found." OnRowCommand="gvVessel_RowCommand"
OnSelectedIndexChanged="gvVessel_SelectedIndexChanged" DataKeyNames="VesselID" >
<asp:/GridView>
Code Behind:
protected void gvVessel_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Convert.ToInt16(gvVessel.SelectedDataKey.Value);
Cache["index"] = index;
Response.Redirect("VesselDraft.aspx");
}
Why isn't the event firing?
回答1:
As suggested in one of the SO questions
GridView OnSelectedIndexChanged event not firing
Gridview selectedindex changed not firing on first click
gridview SelectedIndexChanged Event is not firing.I am using asp.net 4.0 .By the way rowdatabound event is firing perfectly
If you are just clicking on the row in the GridView, that will not fire the event. You have to have some kind of button in the row to click on, which will fire the RowCommand event, as well as the SelectedIndexChanged event (if the row you click is not already selected, of course). It's not exactly like the Windows Forms DataGridView =)
The easiest way to get the event to fire, is to add this attribute to your GridView markup:
AutoGenerateSelectButton="True"
This creates a "Select" LinkButton, which will fire the Gridview1_SelectedIndexChanged2 event in your code-behind when you click it.
EDIT: Just to clarify, this is where you need to add that attribute:
<asp:GridView ID="GridView1" runat="server" GridLines="None"
Width="930px" CellPadding="4" ForeColor="#333333"
onselectedindexchanged="GridView1_SelectedIndexChanged2"
AutoGenerateSelectButton="True" >
回答2:
Please set property
AutoPostback ="true" for GridView.
Thanks, Hitesh
来源:https://stackoverflow.com/questions/14949865/onselectedindexchanged-not-firing-in-gridview