那位朋友 可以帮我看看 我想通过添加一列按钮(buttonfield)实现批准用户注册,其中状态表示用户是否被批准,如果状态是0就是待批准的,如果状态是1,表明管理员已经批准了,现在,我不知如何编写buttonfield事件,来获取当前行的id,然后修改数据库,我看了很多的帖子,但是我都没有运行起来,希望大家帮帮我,谢谢了.......!
我用的是:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
     {
         if (e.CommandName == "a")//a 表示buttonfield的相关CommandName名称为a
         { datastring w = new datastring();//创建连接数据库字符串
           using (SqlConnection coon = new SqlConnection(w.coonstring))
           {  
             coon.Open();
            ........................ 我想问下这里如何实现获得修改当前选中行id(编号),然后修改批准状态的
             coon.Close();
           }
           ShowDB();//重新绑定数据库
         }
     } 

解决方案 »

  1.   

    int intRow = int.Parse(e.CommandArgument.ToString()); //获取当前所在行
      string str = grd.DataKeys[intRow].Value.ToString();
      

  2.   

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "save_btn")   //如果是OpenRow按纽
            {
                //string sql;
                int iIndex = Convert.ToInt32(e.CommandArgument);
                string nID = this.GridView2.DataKeys[iIndex].Value.ToString();
                strsql88 = "select * from stone_out where storage_code='" + nID + "'";
               /* SqlCommand sqlcom = new SqlCommand(sql, con);
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = sqlcom;
                DataSet ds = new DataSet();
                sda.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();*/
                GridView1.EditIndex = -1;
                Databind88(strsql88);
            }
    <asp:ButtonField ButtonType="Button" DataTextField="storage_code" 
                                HeaderText="查看" Text="按钮" CommandName="save_btn" 
                                DataTextFormatString="查看"/>
      

  3.   

    设置DataKeyNames属性
    这个是必须的<%@ Page Language="C#" EnableViewState="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!Page.IsPostBack)
        {
          System.Data.DataTable dt = new System.Data.DataTable();
          System.Data.DataRow dr;
          dt.Columns.Add(new System.Data.DataColumn("id", typeof(System.Int32)));
          dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));
          dt.Columns.Add(new System.Data.DataColumn("Count", typeof(System.Double)));
          dt.Columns.Add(new System.Data.DataColumn("CreateDate", typeof(System.DateTime)));      System.Random rd = new System.Random();
          for (int i = 0; i < 10; i++)
          {
            dr = dt.NewRow();
            dr[0] = i + i;
            dr[1] = "【孟子E章】" + i.ToString();
            dr[2] = System.Math.Ceiling(rd.NextDouble() * 1000);
            dr[3] = DateTime.Now.AddDays(rd.Next(100) - rd.Next(100));
            dt.Rows.Add(dr);
          }
          GridView1.DataSource = dt;
          GridView1.DataBind();
        }
      }  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
      {
        if (e.CommandName == "a")
        {
          Response.Write(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
        }
      }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" DataKeyNames="id">
        <Columns>
          <asp:ButtonField ButtonType="Button" CommandName="a" Text="点击测试得到该行 id 的值" />
        </Columns>
      </asp:GridView>
      </form>
    </body>
    </html>
      

  4.   

    你也可以参考
    http://dotnet.aspx.cc/article/0e8fdeb4-c461-4f44-9933-672a7510097a/read.aspx
    的方法,更适合编辑、修改数据
      

  5.   

    小弟 感激 谢谢 ~!谢谢csdn 平台