使用取消图像从GridView中删除行

时间:2014-01-14 05:16:27

标签: c# asp.net

你好朋友我有一个要求,我想从图像按钮删除我的gridview中删除一行。 我写这样的代码

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" Width="200" GridLines="None" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowDeleting="GridView2_RowDeleting" >
                                                <Columns>
                                                    <asp:TemplateField HeaderText="Sl.No">
                                                        <ItemTemplate>
                                                            <%# Container.DataItemIndex + 1 %>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Supportiong Documents">
                                                        <ItemTemplate>
                                                            <asp:Label ID="lblSupportingDocument" runat="server" Text='<%#Eval("SupportingDocument") %>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Delete" ShowHeader="false">
                                                        <ItemTemplate>
                                                            <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/images/delete.png" CommandName="Cancel" />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>

                                                </Columns>
                                            </asp:GridView>

我背后的代码是

 protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        {
           if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
            {
                bind_SupportingDocumentGrid(id);

            }

        }
    }

void bind_SupportingDocumentGrid(int id)
    {
        List<TblFinancialTransactionSupportDocumentDetail> lstFTSD = ServiceAccess.GetProxy().GetAllFinancialTransactionSupportDocumentDetails();
        var x = (from y in lstFTSD
                 where y.FinancialTransactionId == id
                 select new
                 {
                     y.SupportingDocument
                 }).ToList();
        GridView2.DataSource = x;
        GridView2.DataBind();
    }
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        List<TblFinancialTransactionSupportDocumentDetail> lstFTSD = ServiceAccess.GetProxy().GetAllFinancialTransactionSupportDocumentDetails();
        Label lblSupportingDocument = (Label)GridView2.Rows[e.RowIndex].FindControl("lblSupportingDocument");
        var x = (from y in lstFTSD
                 where y.FinancialTransactionId == Convert.ToInt32(Request.QueryString["id"]) &&
                 y.SupportingDocument == (lblSupportingDocument).ToString()
                 select new
                 {
                     y.FinancialTransactionSupportDocumentDetailId
                 }).ToList();
        ServiceAccess.GetProxy().DeleteFinancialTransactionSupportDocumentDetail(Convert.ToInt32(x));
        bind_SupportingDocumentGrid(Convert.ToInt32(Request.QueryString["id"]));
    }

但不知何故它现在正在工作,我发现使用断点是“GridView2_RowDeleting”事件没有生成。 亲切的帮助我克服这个问题。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

你应该有一个CommandName为“Delete”的按钮:

<asp:TemplateField HeaderText="Delete" ShowHeader="false">
   <ItemTemplate>
      <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/images/delete.png" **CommandName="Delete"** />
   </ItemTemplate>
</asp:TemplateField>

答案 1 :(得分:1)

尝试提供句柄以及事件

protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e) Handles YourDeletebutton.click