Gridview中删除数据时的提示功能


首先给Gridview添加一个模板列,
 
 <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbtDelete" runat="server" CausesValidation="False" CommandName="Delete">删除</asp:LinkButton>
                </ItemTemplate>
</asp:TemplateField>
给这个LinkButton加上OnClientClick代码,结果如下(一定注意CommandName="Del"不能为关键字"Delete"否则会出现GridView“Gridview1”激发了未处理的事件“RowDeleting”)
<asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbtDelete" runat="server" CausesValidation="False" CommandName="Del"  OnClientClick="return confirm('确定要删除该信息吗?')" Text='删除' CommandArgument='<%# Eval("TitleID") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
后台处理代码:
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int titleID = int.Parse(e.CommandArgument.ToString());
        switch (e.CommandName)
        {
            case "Del":
                BTitle = new UestcHouse.BLL.Title();
                BTitle.Delete(titleID);
                Getdata("");
                break;
            default:
                break;
        }
    }
   #region 获得数据的私有方法
    private void Getdata(string strWhere)
    {
        BTitle = new UestcHouse.BLL.Title();
        ds = BTitle.GetModelList(strWhere);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    #endregion

你可能感兴趣的:(职场,休闲)