网格视图代码:
<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条答案
按热度按时间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
标记中:这将创建一个“Select”
LinkButton
,当您单击它时,它将在代码隐藏中激发Gridview1_SelectedIndexChanged2
事件。**EDIT:**为了澄清,您需要在此处添加该属性:
ajsxfq5m2#
请设置属性
谢谢你,Hitesh