protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Agree")
        {
            int iIndex = Convert.ToInt16(e.CommandArgument);
            //try
            //{
                string studentid = GridView1.DataKeys[iIndex].Value.ToString();
                using (SqlConnection conn = new SqlConnection(sConnectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.Connection = conn;
                        cmd.CommandText = "update StudentDeferPaymentApplicationTBSet set AllowedDeferedPayment=@allow where StudentID='@sid'";
                        cmd.Parameters.AddWithValue("@allow", 1);
                        cmd.Parameters.AddWithValue("@sid", studentid);
                        cmd.ExecuteNonQuery();
                        SetBind();
                    }
                }
            //}
            //catch { }
        }
    }
<asp:TemplateField HeaderText="审批">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="false"
                            CommandName="Agree" Text="审批" OnClientClick="return confirm('确定同意审批吗?')" CommandArgument='<%# Container.DataItemIndex %>'/>
                    </ItemTemplate>
                </asp:TemplateField>大家帮看一下哈, 上面这些代码有没有错误呢?

解决方案 »

  1.   

    接上,我已有添加GridView的RowCommand事件,运行后点击审批按钮,相应的表格却没有更新,这是怎么回事了?
      

  2.   

    cmd.CommandText = "update StudentDeferPaymentApplicationTBSet set AllowedDeferedPayment=@allow where StudentID=@sid";
    CommandArgument='<%# Eval("StudentID")%>'/>
           
      

  3.   

    有重写绑定了,Setbind()就是重新绑定的。
    而我的问题也不在绑定上, 根本是: GridView1_RowCommand里的代码好像从没被执行过一样。
    所以,更新操作一直没执行。
    我真不知道问题出在哪个环节。
      

  4.   

    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow row = this.GridView1.Rows[index];
    string studentid = row.Cells[0].Text;
      

  5.   

    int index = Convert.ToInt32(e.CommandArgument);
    改成
    int index  = this.GridView1.DataKeys[e.Row.DataItemIndex][0];
      

  6.   

    设置了断点后,按F5或菜单中的“调试”->"启动调试"
      

  7.   

    自己认为在if (e.CommandName == "Agree")这个条件没符合,里面的语句真接跳过
    但就我的理解, 觉得应该if语句是正确的。
    有没有哪里的细节我没不懂的, 哪位高人指出来帮帮忙哈……T
      

  8.   

    那是肯定的,你代码写错了
    你用的是模板,当然要先找到那个button了
      

  9.   

    你的那种后台代码,对应的前台代码应该是 <asp:ButtonField CommandName="Agree" Text="审批" />
      

  10.   

    我重新全部发动了一下,还是不行, 数据库里表更新操作还是没执行。再次附上代码。protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Agree")
            {
                int iIndex = Convert.ToInt16(e.CommandArgument);
                //int index = Convert.ToInt32(e.CommandArgument);
                //GridViewRow row = this.GridView1.Rows[index];            //string studentid = row.Cells[0].Text;
                //try
                //{
                string studentid = GridView1.DataKeys[iIndex].Value.ToString();
                using (SqlConnection conn = new SqlConnection(sConnectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.Connection = conn;
                        cmd.CommandText = "update StudentDeferPaymentApplicationTBSet set AllowedDeferedPayment=@allow where StudentID=@sid";
                        cmd.Parameters.AddWithValue("@allow", 1);
                        cmd.Parameters.AddWithValue("@sid", studentid);
                        cmd.ExecuteNonQuery();
                        SetBind();
                    }
                }
                //}
                //catch { }
            }
        }
        public void SetBind()
        {
            DataSet ds = new DataSet();
            //try
            //{
            using (SqlConnection conn = new SqlConnection(sConnectionString))
            {
                //conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter("SELECT StudentID, ApplyDate, ApplyCause FROM StudentDeferPaymentApplicationTBSet where AllowedDeferedPayment=0", conn);
                sda.Fill(ds);
            }
            GridView1.DataSource = ds;
            GridView1.DataBind();
            //}
            //catch { }
        }
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="StudentID" HeaderText="学生学号" />
                    <asp:BoundField DataField="ApplyDate" HeaderText="申请日期" />
                    <asp:BoundField DataField="ApplyCause" HeaderText="申请原因" />
                    <asp:ButtonField ButtonType="Button" CommandName="Agree" Text="审批" />
                </Columns>
            </asp:GridView>哪位好心人,再帮帮看看哈…… 我实在是不知道到底错在哪儿啊……?
      

  11.   

    晕啊, <asp:GridView runat="server" ID="topicManageGV" AutoGenerateColumns="False" CssClass="table" AllowPaging="true" Width="100%" OnRowDataBound="topicManageGV_RowDataBound" OnRowCommand="topicManageGV_RowCommand">
                <Columns>
                    <asp:BoundField DataField="id"/>
                    <asp:BoundField DataField="UserName"/>
                    <asp:BoundField DataField="RealName"/>
                    <asp:BoundField DataField="Mobile"/>
                    <asp:BoundField DataField="Email"/>
                    <asp:BoundField DataField="office_name" />
                    <asp:ButtonField CommandName="sel" Text="查看" />
                    <asp:ButtonField CommandName="edit" Text="修改" />
                    <asp:ButtonField CommandName="qxcom" Text="分配权限" />
                    <asp:ButtonField CommandName="del" Text="删除" />
                </Columns>
            </asp:GridView>protected void topicManageGV_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow row = null;
            int index = -1;
            switch (e.CommandName)
            { 
                case "sel":
                    index = Convert.ToInt32(e.CommandArgument);
                    row = this.topicManageGV.Rows[index];
                    string selId = row.Cells[0].Text;
                    Response.Redirect("ManagerView.aspx?id=" + selId);
                    break;
                case "edit":
                    index = Convert.ToInt32(e.CommandArgument);
                    row = this.topicManageGV.Rows[index];
                    string editId = row.Cells[0].Text;
                    Response.Redirect("ManagerEdit.aspx?id=" + editId);
                    break;
                case "del":
                    index = Convert.ToInt32(e.CommandArgument);
                    row = this.topicManageGV.Rows[index];
                    string delId = row.Cells[0].Text;
                    Graduate.BLL.Manager bll = new Graduate.BLL.Manager();
         
                    bll.Delete(int.Parse(delId));
                    break;
                case "qxcom":
                    index = Convert.ToInt32(e.CommandArgument);
                    row = this.topicManageGV.Rows[index];
                    string qxid = row.Cells[0].Text;
                    Response.Redirect("../system/DealPermission.aspx?id=" + qxid);
                    break;
            }
            BindGV();
        }
      

  12.   

    解决 了哈,。谢谢大家哈。参考了19#的代码才发现了我出现了低级的错误。
    真是郁闷。 刚开始以为有加载 RowCommand事件,实际上是没加载正确,加载的是一个空的RowCommand2事件。真正的RowCommad事件却没加载。