Question

I have a nested gridview of images that are associated with the parent grid's row on a one to many relationship. This structure is contained in a user control. The images are uploaded with a fileupload control and are stored in a folder on the server. When the image grid's row is deleted, I delete the image from the server in the OnRowDeleting event. What's weird is that when you delete a row, a postback occurs and the OnRowDeleting event fires, the record is deleted from the database, but the row does not go away from the UI until you then delete it a second time. What's even weirder is that when I comment out the code in the OnRowDeleting event, the row deletes immediately. What's going on here?

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

I commented out any containing updatepanels in the parent page as they can always be the culprit, but there was no effect.

Here is the OnRowDeleting event:

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();
    }
}

I've done this before and have had no issues, so any help would be greatly appreciated!

Was it helpful?

Solution

I ended up wrapping the nested GridView ("gvImages"), the FileUpload, and Button in a user control, and now it works great. I didn't change any of the code, just wrapped it all in the user control, so go figure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top