asp.net OnSelectedIndexChanged未在网格视图中触发

krcsximq  于 2022-11-19  发布在  .NET
关注(0)|答案(2)|浏览(158)

网格视图代码:

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

为什么事件未激发?

vfh0ocws

vfh0ocws1#

如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窗体DataGridView =不完全一样。
触发事件的最简单方法是将此属性添加到GridView标记中:

AutoGenerateSelectButton="True"

这将创建一个“Select”LinkButton,当您单击它时,它将在代码隐藏中激发Gridview1_SelectedIndexChanged2事件。

**EDIT:**为了澄清,您需要在此处添加该属性:

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

ajsxfq5m2#

请设置属性

AutoPostback ="true" for GridView.

谢谢你,Hitesh

相关问题