使用“取消图像”按钮从GridView中删除行 [英] Delete Row From GridView using cancel Image button

查看:71
本文介绍了使用“取消图像”按钮从GridView中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

hello friend i have a requirement in which i want to delete a row from my gridview on image button delete. i write the code like this

<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>





和后面的代码是



and my code behind is

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事件没有生成。亲切的帮助我克服这个问题。

提前感谢。

:)



but somehow it is now working and i found using breakpoint is "GridView2_RowDeleting" event is not generating. kind help me to overcome from this problem.
thanks in advance.
:)

推荐答案

请参阅下面的链接将获得您想要的内容: -

http://www.aspsnippets。 com / Articles / ASPNet-GridView --- Delete-Row-with-Confirmation.aspx [ ^ ]
refer below link will get what you want exactly:-
http://www.aspsnippets.com/Articles/ASPNet-GridView---Delete-Row-with-Confirmation.aspx[^]


它不会执行因为你没有添加任何命令字段

Its not going to execute coz u have not added any commandfields
<asp:commandfield showdeletebutton="True" />





所以你必须写一下图片按钮内部点击事件



试一下,让我知道..



so u have to write that inside click event of image button

try it and let me know..


<ItemTemplate>
    <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/images/delete.png" CommandName="Cancel" />
</ItemTemplate>



当您使用 ImageButton 并且您有一个 CommandName ,所以你应该处理 GridView.RowCommand 事件 [ ^ ]。



为此声明 OnRowCommand in GridView 标记。


As you are using ImageButton and you have a CommandName, so you should handle the GridView.RowCommand Event[^].

For that declare OnRowCommand in GridView Markup.

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" Width="200" GridLines="None" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowCommand="GridView2_RowCommand" >



然后在Code Beh上定义事件喜欢......


Then define the Event on Code Behind like...

protected void GridView2_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName == "Cancel")
    {
        // Write your Delete Logic.
    }
}


这篇关于使用“取消图像”按钮从GridView中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆