OnSelectedIndexChanged在GridView中不触发

问题描述 投票:1回答:2

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");
}

为什么不发射事件?

c# asp.net .net gridview datagridview
2个回答
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" >

0
投票

请设置属性

AutoPostback ="true" for GridView.

谢谢,Hitesh

© www.soinside.com 2019 - 2024. All rights reserved.