获取gridview主键,利用主键找出数据库某一字段
在gridview中有一个linkbutton,在他的双击事件 protected void lnkBtnDelete_Click(object sender, EventArgs e)中想要找到主键Admin_ID,根据主键Admin_ID找到相应数据库的某一个字段Admin_type,再根据Admin_type的值决定是否要上出gridview这一行gridview 主键 --------------------编程问答--------------------
protected void lnkBtnDelete_Click(object sender, EventArgs e)--------------------编程问答--------------------
{
// int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
// string id = this.gridView.DataKeys[row].Value.ToString();
// GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
string id = gridView.DataKeys[((GridViewRow)sender).RowIndex].Values[0].ToString();
DataSet ds = bll.GetList("Admin_ID='" + id + "'");
DataTable dt = ds.Tables[0];
if (dt.Rows[0]["Admin_type"].ToString() == "1")
{
Response.Write("<script language=javascript>alert('超级管理员无法删除!');location='AdminManagement.aspx';</script>");
}
else if (bll.Delete(id))
{
Response.Write("<script language=javascript>alert('删除你麻痹!');location='AdminManagement.aspx';</script>");
}
else
{
Response.Write("<script language=javascript>alert('删除失败!');location='AdminManagement.aspx';</script>");
}
}
<asp:TemplateField HeaderText="操作">--------------------编程问答-------------------- 菜鸟求助啊,感激不尽 --------------------编程问答--------------------
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDelete" runat="server" onclick="lnkBtnDelete_Click" CommandArgument='<%# Eval("Admin_ID") %>'>删除</asp:LinkButton>
<a href='AdminEdit.aspx?Admin_ID=<%#Eval("Admin_ID") %>'>编辑</a>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
protected void LinkButton1_Click(object sender, EventArgs e)
{
string id = ((LinkButton)sender).CommandArgument.ToString();
int rowindex = (((LinkButton)sender).Parent.Parent as GridViewRow).RowIndex; //当前行
Label labprotype = GridViewConfig.Rows[rowindex].FindControl("labprotype") as Label; //类型
if(labprotype.Text=="1")
//删除
else
//不删
}
前台GridView中:
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
Text="删除" OnClick="LinkButton1_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
你给它加上一个删除前提示,应该就可以用了。,。...
--------------------编程问答-------------------- System.NullReferenceException: 未将对象引用设置到对象的实例。
--------------------编程问答--------------------
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 48: int rowindex = (((LinkButton)sender).Parent.Parent as GridViewRow).RowIndex; //当前行
行 49: Label labprotype = gridView.Rows[rowindex].FindControl("labprotype") as Label; //类型
行 50: if (labprotype.Text == "1")
行 51: {
行 52: Response.Write("<script language=javascript>alert('超级管理员无法删除!');location='AdminManagement.aspx';</script>"); --------------------编程问答-------------------- 你给的代码里没有用到Adimn_type啊?
补充:.NET技术 , ASP.NET