你不會在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