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");
}
为什么不发射事件?
如其中一个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" >
请设置属性
AutoPostback ="true" for GridView.
谢谢,Hitesh