简体   繁体   中英

OnSelectedIndexChanged Not Firing in GridView

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?

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" >

Please set property

AutoPostback ="true" for GridView.

Thanks, Hitesh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM