GridView中删除的时候如何获得这条数据的主键?

解决方案 »

  1.   

    页面LOAD的时候设置主键的字段名称
    如:this.dl_news.DataKeyField="hbtd_NewsId";当你需要获取的时候使用:
    比如需要删除的时候获取当前行的ID:
    string newsid=this.dl_news.DataKeys[e.Item.ItemIndex].ToString();我的删除使用的方法如下:
    /**
     * 删除新闻
     * 
     * */
    private void dl_news_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    {
      ImageButton button=(ImageButton)e.Item.FindControl("imgbtn"); if(button.CommandName=="delete")
    {
    string newsid=this.dl_news.DataKeys[e.Item.ItemIndex].ToString();

    if(newshdl.delNews(newsid,1))
    {
    Response.Write("<script>alert('删除成功!');window.location.href='news_modify.aspx';</script>");
    }
    else
    {
    Response.Write("<script>alert('删除失败!');window.location.href='news_modify.aspx';</script>");
    }

    }
    }
      

  2.   

    在linkbutton控件中绑定ID值 CommandArgument='<%#Eval("ID")%>'
    在后台RowCommand的事件中e.CommandArgument.toString()得到绑定的值