在GridView中一般不支持命令列 只支持系统自带的Update Delete Edit Cancel命令列
其实要自定义命令列也非常简单的
前台
<
asp:TemplateField
>
< ItemTemplate >
< asp:LinkButton runat = " server " ID = " lbJoin " CommandName = " joinSchool " Text = " 入 " CommandArgument = ' <%#DataBinder.Eval(Container,"RowIndex") %> ' OnCommand = " lbJoin_Command " CausesValidation = " false " ></ asp:LinkButton >
</ ItemTemplate >
</ asp:TemplateField >
< ItemTemplate >
< asp:LinkButton runat = " server " ID = " lbJoin " CommandName = " joinSchool " Text = " 入 " CommandArgument = ' <%#DataBinder.Eval(Container,"RowIndex") %> ' OnCommand = " lbJoin_Command " CausesValidation = " false " ></ asp:LinkButton >
</ ItemTemplate >
</ asp:TemplateField >
前台把需要的参数邪道CommandArgument中 在后台去获取
后台:
protected
void
lbJoin_Command(
object
sender,CommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
int sch_id = Convert.ToInt32(SchoolView.DataKeys[rowIndex].Value);
if (Users.SetSchoolId(Convert.ToInt32(Session["UserID"]), sch_id))
{
Session["S_ID"] = sch_id;
Response.Redirect("FindClass.aspx");
}
else
{
insertHint.InnerHtml="<font color='red'>加入学校失败.请重试..</font>";
}
}
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
int sch_id = Convert.ToInt32(SchoolView.DataKeys[rowIndex].Value);
if (Users.SetSchoolId(Convert.ToInt32(Session["UserID"]), sch_id))
{
Session["S_ID"] = sch_id;
Response.Redirect("FindClass.aspx");
}
else
{
insertHint.InnerHtml="<font color='red'>加入学校失败.请重试..</font>";
}
}
后台通过e.CimmandArgument获取参数 在此处为命令列所在的RowIndex值 根据此值获取主键参数 进行数据库操作
其实比较简单 只要设置好了参数就OK了.....