OnRowDeleting事件后未嵌套(完全)删除嵌套的gridview行

时间:2013-09-27 01:32:06

标签: c# asp.net gridview rowdeleting nested-controls

我有一个嵌套的图像视图,它与一对多关系中的父网格行相关联。该结构包含在用户控件中。图像使用文件上载控件上载,并存储在服务器上的文件夹中。删除图像网格的行时,我会在OnRowDeleting事件中从服务器中删除该图像。奇怪的是,当你删除一行,发生回发并且OnRowDeleting事件触发时,记录将从数据库中删除,但是在你再次删除它之前,该行不会离开UI。甚至更奇怪的是,当我在OnRowDeleting事件中注释掉代码时,该行会立即删除。这是怎么回事?

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="OrderItemView.ascx.cs" Inherits="Controls_OrderItemView" EnableTheming="true" EnableViewState="true"%>

<asp:GridView ID="OrderItemList" runat="server" AutoGenerateColumns="False"
    DataKeyNames="Id,ConcurrencyId" DataSourceID="OrderItemDataSource" 
    SkinID="Blue" OnRowDataBound="OrderItemList_RowDataBound" 
    EnableModelValidation="True" Width="100%"
    OnRowUpdating="OrderItemList_RowUpdating"
    OnRowUpdated="OrderItemList_RowUpdated" AllowSorting="True" >
    <Columns>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:ImageButton ID="ImageButtonConfirmDelete" runat="server" 
                    CausesValidation="False" CommandName="Delete" ImageUrl="../Images/Grid_ActionDelete.gif"
                    OnClientClick='return confirm("Are you sure you want to delete this Order Item? This cannot be undone.");'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item Name" SortExpression="OrderItemName">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("OrderItemName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Images">
            <ItemTemplate>
                <a href="" data-id='<%# Eval("id","imageHeader_{0}") %>' class="expandImages">
                    <asp:Label ID="Label3" runat="server"
                        Text='<%# string.Format("{0} Image{1}", Helpers.GetImageCount(new Guid(Eval("Id").ToString())), Helpers.GetImageCount(new Guid(Eval("Id").ToString())) != 1 ? "s" : "") %>'>
                    </asp:Label>
                </a>
                <div id='<%# Eval("id","images_{0}") %>' class="imageDisplay">
                    <asp:GridView ID="gvImages" runat="server" SkinID="Blue"
                        DataKeyNames="Id" EnableModelValidation="True"
                        DataSourceID="ImageDataSource" AutoGenerateColumns="False"
                        ShowHeader="False" onrowdeleting="gvImages_RowDeleting">
                        <Columns>
                            <asp:TemplateField ShowHeader="false">
                                <ItemTemplate>
                                    <asp:ImageButton ID="ImageButtonConfirmDeleteImage" runat="server" 
                                        CausesValidation="False" CommandName="Delete" ImageUrl="../Images/Grid_ActionDelete.gif"/>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField ShowHeader="false">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FileName") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <a href="" data-id='<%# Eval("id","image_{0}") %>' onclick='showImage("<%# Eval("ImageUrl") %>"); return false;'>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("FileName") %>' ToolTip="Click to Preview Image"></asp:Label>
                                    </a>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <div style="color: Blue; font-style: italic;">No Images</div>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:FileUpload ID="FileUpload1" runat="server"/>
                    <asp:Button ID="btnUploadImage" runat="server" Text="Upload" CommandArgument='<%# Eval("Id") %>'
                        CommandName="orderItemId" oncommand="btnUploadImage_Command"/>
                    <asp:ObjectDataSource ID="ImageDataSource" runat="server" 
                        DataObjectTypeName="OrderSite.Entities.OrderItemImage" 
                        DeleteMethod="Delete" InsertMethod="Save" 
                        OldValuesParameterFormatString="original_{0}" SelectMethod="GetList" 
                        TypeName="OrderSite.Bll.OrderItemImageManager" UpdateMethod="Save"
                        SelectCountMethod="SelectCountForGetList">
                        <SelectParameters>
                            <asp:Parameter DbType="Guid" Name="orderItemId"/>
                        </SelectParameters>
                    </asp:ObjectDataSource>
                </div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
        <div style="padding: 3px 0 3px 3px 3px;">No Order Items Exist</div>
    </EmptyDataTemplate>
</asp:GridView>
<asp:ObjectDataSource ID="OrderItemDataSource" runat="server" 
    DataObjectTypeName="OrderSite.Entities.OrderItem" 
    DeleteMethod="Delete" InsertMethod="Save" 
    OldValuesParameterFormatString="original_{0}" SelectMethod="GetList" 
    TypeName="OrderSite.Bll.OrderItemManager" UpdateMethod="Save"
    OnInserting="OrderItemDataSource_OnInserting"
    SortParameterName="sortExpression" SelectCountMethod="SelectCountForGetList">
    <SelectParameters>
        <asp:Parameter DbType="Guid" Name="orderFormId" />
        <asp:Parameter Name="sortExpression" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

我在父页面中注释掉了任何包含更新面板的内容,因为它们始终是罪魁祸首,但没有任何效果。

这是OnRowDeleting事件:

protected void gvImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    // Also delete the file from the images folder
    OrderItemImage myImage = OrderItemImageManager.GetItem((Guid)e.Keys["Id"]);
    if (myImage != null)
    {
        string path = string.Format("../Images/OrderItemImages/{0}", myImage.OrderItemId.ToString());
        if (File.Exists(Server.MapPath(string.Format("{0}/{1}", path, myImage.FileName))))
        {
            File.Delete(Server.MapPath(string.Format("{0}/{1}", path, myImage.FileName)));
            // if there are no images left in folder, then delete it (only deletes empty directory)
            if (Directory.GetFiles(Server.MapPath(path), "*.*", SearchOption.TopDirectoryOnly).Length == 0)
                Directory.Delete(Server.MapPath(path));
        }
        OrderItemList.DataBind();
    }
}

我以前做过这个并且没有任何问题,所以非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我最终在用户控件中包装了嵌套的GridView(“gvImages”),FileUpload和Button,现在它运行得很好。我没有更改任何代码,只是将其全部包装在用户控件中,所以请参阅图。