OnSelectedIndexChanged在GridView中不触发

时间:2013-02-19 04:29:30

标签: c# asp.net .net gridview datagridview

Gridview代码:

<asp:GridView ID="gvVessel" runat="server" AutoGenerateColumns="false" GridLines="None"
     EmptyDataText="No Vessels found." OnRowCommand="gvVessel_RowCommand"      
     OnSelectedIndexChanged="gvVessel_SelectedIndexChanged" DataKeyNames="VesselID" >
<asp:/GridView>

代码背后:

protected void gvVessel_SelectedIndexChanged(object sender, EventArgs e)
{
    int index = Convert.ToInt16(gvVessel.SelectedDataKey.Value);
    Cache["index"] = index;

    Response.Redirect("VesselDraft.aspx");
}

为什么不发射事件?

2 个答案:

答案 0 :(得分:0)

如其中一个SO问题所示 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

如果您只是点击GridView中的行,则不会触发该事件。您必须在行中使用某种按钮才能点击,这将触发RowCommand事件以及SelectedIndexChanged事件(如果您单击的行当然尚未被选中) 。它不完全像Windows Forms DataGridView =)

让事件触发的最简单方法是将此属性添加到GridView标记中:

AutoGenerateSelectButton="True"

这会创建一个“选择”LinkButton,当您点击它时会触发代码隐藏中的Gridview1_SelectedIndexChanged2事件。

编辑:为了澄清,您需要添加该属性:

<asp:GridView ID="GridView1" runat="server" GridLines="None" 
  Width="930px" CellPadding="4" ForeColor="#333333"  
  onselectedindexchanged="GridView1_SelectedIndexChanged2"
  AutoGenerateSelectButton="True" >

答案 1 :(得分:0)

请设置属性

AutoPostback ="true" for GridView.

谢谢, 亚太区首席技术官Matt