你不会在ButtonClick事件得到的SelectedIndex
所以尝试这样
protected void imgbtn_Click(object sender, EventArgs e)
{
try {
// Get the button that raised the event
LinkButton lnkbtn = (LinkButton)sender;
// Get the row that contains this button
GridViewRow gvRow = (GridViewRow)lnkbtn.NamingContainer;
// Get rowindex
int i = gvRow.RowIndex;
Label t = (Label)GridView1.Rows[i].Cells[0].FindControl("Label1");
Session["course"] = t.Text;
Response.Redirect("registration.aspx");
}
} catch (Exception ex) {
}
}
protected void grdview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
try {
if ((e.CommandName == "ImageButton")) {
// Get RowIndex
int RowIndex = Convert.ToInt32(e.CommandArgument);
// Get Row
GridViewRow gvr = grdPhone.Rows(RowIndex);
}
} catch (Exception ex)
{
}
}
ASPX:
在GridView的
你需要设置的CommandName和论证像此
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" OnClick="imgbtn_Click" CausesValidation="false" CommandName="ImageButton" CommandArgument='<%# Container.DataItemIndex %>'>Edit</asp:ImageButton >
</ItemTemplate>
</asp:TemplateField>
单击该按钮不一定选择该行。看看如何添加图像,以及如何在点击图像按钮时强制选择该行。 – Kangkan
我已经使用图像按钮作为gridview中的模板字段。 – user3065219