数据库中的表Reply的主键是replyID,外键是cardID。请问要在GridView模板项中使用自定义按钮删除记录,应该怎么做?

解决方案 »

  1.   

    编辑列->添加一个ButtonField 设他的CommandName 为一个名字“del”
    在GridView 中的RowCommand事件中
    if(e.commandName=="del")
    {
        这儿写你要执行的东西
    }
      

  2.   

    对不起,是我表达不清楚,因为要控制格式,所以要使用模板项,但是在添加了Button按钮后,不知道接着Button控件的代码应该怎么写,特别是怎么取得所在行的replyID
      

  3.   

    Button控件的代码应该写执行删除的语句了,你上面说的应该是主从表的删除了,那应该把子表的删除语句放在前面,主表的删除语句放在后面执行。
      

  4.   

    你在GridView里的DataKeyNames="replyID" 
    后台 GridView.DataKeys[当前行索引].Value.ToString();
    这样你Button里就可以取到呢replyID的值了
      

  5.   

     <asp:TemplateField HeaderText="Delete">
                                                <ItemTemplate>
                                                    <asp:ImageButton ID="IBtnDelete" runat="server" ImageUrl="~/images/toolBar/delete.gif"
                                                        CommandArgument='<%# Eval("RequList_id") %>' ForeColor="blue" CommandName="Delete"
                                                        OnCommand="DeleteClick" OnClientClick="return confirm('确定删除吗? 数据一旦删除将无法恢复!!');" />
                                                </ItemTemplate>
                                                <ItemStyle Width="50px" />
                                                <HeaderStyle HorizontalAlign="Center" />
                                            </asp:TemplateField>
        protected void DeleteClick(object sender, CommandEventArgs e)
        {
            if (e.CommandName.Equals("Delete"))
            {
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                ONETIMS.BusinessRules.FAEManager.FAE_RequList bill = new ONETIMS.BusinessRules.FAEManager.FAE_RequList();
                int State = Convert.ToInt32(bill.ReturnStateById(id));
                if (State != 1)
                {
                    Web.Utility.JavascriptDeal.AjaxShow("对不起该申请已经在处理当中不能删除!");
                }
                else
                {
                    bill.Delete(id);
                }
                this.BindGridView();
            }
        }