_________________________________________
是否购买|试剂编号|试剂名称|库存|单价....
------------------------------------------
 购买   253145    得灵试剂 10  600
我在GridView中加入一按钮列,CommandName为"add",我要点击"购买",然后取这一行的试剂编号的值保存的哈稀表中传到另一个页面,老是出错,不知道怎搞,刚用asp2.0,有点晕.  protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
    {       
        if (e.CommandName == "add")
        { 
                   
        string reId = ?这里的试剂编号怎么取,用了很多方法,总是不行,在asp1.0中,只要设置表格的DataKeyField="re_id",然后在用DataKeys取编号这个值,2.0怎么不行啊,请各位帮帮忙?
           
            if (Session["bus"] == null)
            {
                Hashtable ht = new Hashtable();
               ht.Add(reId, 1);
               Session["bus"] = ht;
           }
        }
        else
        {
            System.Collections.Hashtable ht = (Hashtable)Session["bus"];
           if (ht[reId] == null)
            {
                ht[reId] = 1;
           }
           else
           {
                ht[reId] = (int)ht[reId] + 1;
            }
            Session["bus"] = ht;
        }
    }

解决方案 »

  1.   

    GridView 需要设置 DataKeyNames 属性为你的表中的主键值.
    当然你可以将 你的购买按钮的 CommandArgument 的值设置为你的主键,
    这样,你即可以使用e.CommandArgument来获取了.
      

  2.   

    DataKeyNames怎么设置? 肯定不是这么设置的吧this.GridView1.DataKeyNames="re_id";
      

  3.   

    你可以这样,在CommadnArgument='<%#Eval('这里绑定你的ID值')%>'
    然后在gridview_RowCommand(object,Arg..)
        {
         if(e.commandName='Add')
          {
                       //处理
         }
    }
      

  4.   

    可是BottonField中没有CommandArgument这个属性
      

  5.   

    怎么会啊
    有CommandArgument这个属性
      

  6.   

    string reId = ?这里的试剂编号怎么取,用了很多方法,总是不行,在asp1.0中,只要设置表格的DataKeyField="re_id",然后在用DataKeys取编号这个值,2.0怎么不行啊,请各位帮帮忙?===========1。
    GridView 中与之对应的是 DataKeyNames ,并且可以设置多个键
    2。// .aspx
    <asp:gridview id="GridView1" datakeynames="re_id" ............
    // .aspx.cs
    if (e.CommandName == "add")
    {
            Control cmdSource = e.CommandSource as Control;
            GridView grd = sender as GridView;
            GridViewRow row = cmdSource.NamingContainer as GridViewRow;
            int rowIndex = row.RowIndex;
            string reId =  grd.DataKeys[rowIndex].Value.ToString();
          
            // .........
    3。
    有用的相关信息:如何在GridView的RowCommand事件中获取当前的GridViewRow 
    http://www.cnblogs.com/Jinglecat/archive/2007/07/05/806460.html
      

  7.   

    到int rowIndex=row.RowIndex提示错误,未将对象引用设置到对象的实例。
      

  8.   

    到int rowIndex=row.RowIndex提示错误,未将对象引用设置到对象的实例。
    ========
    LZ 使用的的是按钮列?Control cmdSource = e.CommandSource as Control;
    GridView grd = sender as GridView;
    GridViewRow row = cmdSource.NamingContainer as GridViewRow;
    int rowIndex = row.RowIndex;>>>int rowIndex = Convert.ToInt32(e.CommandArgument);
    GridView grd = sender as GridView;
    GridViewRow row = grd.Rows[rowIndex];至于个中原因请见
    如何在GridView的RowCommand事件中获取当前的GridViewRow 
    http://www.cnblogs.com/Jinglecat/archive/2007/07/05/806460.html
      

  9.   

    方法一:
    step1:设置你的GridView的DataKeyNames为re_id
    <asp:GridView ID="GridView1" runat="server" DataKeyNames="re_id" ...step2:在RowCreated事件中给按钮的CommandArgument属性赋值为当前行索引
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        LinkButton addButton ;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            addButton = (LinkButton)e.Row.Cells[0].Controls[0];//请确认你的按钮的位置
            if (addButton != null)
            {
                if (addButton.CommandName== "add")
                    addButton.CommandArgument = e.Row.RowIndex.ToString();
            }
        }
    }step3:在RowCommand中得到此行主键列
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {        
        if (e.CommandName == "add")
        {
            Response.Write(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString());
        }
    }