简体   繁体   English

OnSelectedIndexChanged 未在 GridView 中触发

[英]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正如其中一个 SO 问题所建议的那样
GridView OnSelectedIndexChanged event not firing GridView OnSelectedIndexChanged 事件未触发
Gridview selectedindex changed not firing on first click Gridview selectedindex 更改未在第一次单击时触发
gridview SelectedIndexChanged Event is not firing.I am using asp.net 4.0 .By the way rowdatabound event is firing perfectly gridview SelectedIndexChanged 事件未触发。我正在使用 asp.net 4.0 。顺便说一句,rowdatabound 事件正在完美触发

If you are just clicking on the row in the GridView , that will not fire the event.如果您只是单击GridView中的行,则不会触发该事件。 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).您必须在行中有某种按钮才能单击,这将触发RowCommand事件以及SelectedIndexChanged事件(当然,如果您单击的行尚未选中)。 It's not exactly like the Windows Forms DataGridView =)它不完全像 Windows 窗体 DataGridView =)

The easiest way to get the event to fire, is to add this attribute to your GridView markup:让事件触发的最简单方法是将此属性添加到您的GridView标记中:

AutoGenerateSelectButton="True"

This creates a "Select" LinkButton , which will fire the Gridview1_SelectedIndexChanged2 event in your code-behind when you click it.这将创建一个“Select” LinkButton ,当您单击它时,它将在您的代码隐藏中触发Gridview1_SelectedIndexChanged2事件。

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谢谢, 海特什

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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