Solved

Reading FileUpload Control Values From GridView...

Posted on 2010-08-30
4
1,108 Views
Last Modified: 2012-06-21
Hi,
I'm sure this is a simple question, but despite some Googling and implementing the solutions I'm still coming up empty, literally. In a nutshell I have a programmatically developed GridView that has 4 labels, 2 hidden variables, and a FileUpload control on it. The user clicks a button that runs a chunk of code which cycles through the gridview and extracts the 2 values from the hidden variables as well as the file to upload if any were selected. I can successfully grab the hidden variables, but the FileUpload "HasFile" always come back empty even when a file was selected. I've worked with the FileUpload before, but never when placed within a GridView.

The code behind the submit button is below. Any thoughts of what I'm missing or haven't set up correctly? When I step through it it recognizes the "upDocumentTrainingEmployeeCartFile" control, but not that it has a file in it.

Here is my environment setup:
Visual Studio 2010
C#
AjaxControlToolkit
Microsoft .NET Framework Version:4.0.30319
ASP.NET Version:4.0.30319.1


//ASPX page
    <div id="divGvwEmployeeDocumentFileAttachment" style="width:100%; display:none" class="FieldLabels">
        <asp:UpdatePanel ID="upPanelFileCart" runat="server" RenderMode="Inline" UpdateMode="Conditional">
            <ContentTemplate>
                <%-- Programatically generated Grid--%>
                <asp:gridview ID="gvwFileCart"  BorderColor="#3a4f63" BorderStyle="Solid" BorderWidth="1" runat="server"
                     AutoGenerateColumns="False"
                     PageSize="50"  
                     RowStyle-CssClass="FieldLabels"
                     HeaderStyle-BackColor="#72a9d2"
                     AllowPaging="false"
                     EmptyDataRowStyle-BackColor="#f6f9a5"
                     EmptyDataText="&nbsp;&nbsp;&nbsp;File cart is empty..."
                     PagerSettings-Position="Bottom"
                     PagerStyle-BackColor="#c6dae9"
                     PagerStyle-Font-Size="1.50em"
                     PagerStyle-HorizontalAlign="Center"
                     PagerSettings-Mode="NumericFirstLast"
                     PagerSettings-Visible="true"
                     Caption="<b>File Cart</b>"
                     CaptionAlign="Left"
                     RowStyle-Height="22px"
                     EmptyDataRowStyle-Height="20px">

                        <Columns>
                            <%--Row Counter--%>
                            <asp:BoundField ItemStyle-Width="5%" Visible="false" DataField="RowNumber" HeaderText="#" />

                            <%--Column 1, Cell 1--%>
                            <asp:TemplateField ItemStyle-Width="40%" HeaderText="Full Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblBadgeFullNameFile" Text='<%# Eval("Column1") %>' runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>

                            <%--Column 2, Cell 2--%>
                            <asp:TemplateField ItemStyle-Width="30%" HeaderText="Badge No.">
                                <ItemTemplate>
                                    <asp:Label ID="lblBadgeNoFile" Text='<%# Eval("Column2") %>' runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>

                            <%--Column 3, Cell 3--%>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:HiddenField ID="hfIdEmployeeIDFile" Value='<%# Eval("Column3") %>' runat="server"/>    
                                </ItemTemplate>
                            </asp:TemplateField>

                            <%--Column 4, Cell 1--%>
                            <asp:TemplateField ItemStyle-Width="40%" HeaderText="Document No.">
                                <ItemTemplate>
                                    <asp:Label ID="lblDocumentNumberFile" Text='<%# Eval("Column4") %>' runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>

                            <%--Column 5, Cell 2--%>
                            <asp:TemplateField ItemStyle-Width="30%" HeaderText="Rev">
                                <ItemTemplate>
                                    <asp:Label ID="lblRevisionFile" Text='<%# Eval("Column5") %>' runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>

                            <%--Column 6, Cell 3--%>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:HiddenField ID="hfIdDocumentIDFile" Value='<%# Eval("Column6") %>' runat="server"/>    
                                </ItemTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField HeaderStyle-HorizontalAlign="Center" HeaderText="Attach File">
                                <ItemTemplate>
                                    <asp:FileUpload ID="upDocumentTrainingEmployeeCartFile" Visible="true" Width="400px" runat="server" onfocus="this.select();" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                </asp:gridview>
            </ContentTemplate>
         </asp:UpdatePanel>
    </div>

//Event Code
protected void btnSubmitDocumentTraining_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow rowFileCart in gvwFileCart.Rows)
        {
            HiddenField tmp_EMPLOYEE_ID = (HiddenField)rowFileCart.FindControl("hfIdEmployeeIDFile");
            HiddenField tmp_RELEASE_DOC_ID = (HiddenField)rowFileCart.FindControl("hfIdDocumentIDFile");
            FileUpload File_Upload = (FileUpload)rowFileCart.FindControl("upDocumentTrainingEmployeeCartFile");

            //Check to see if a file was selected to upload
            if (File_Upload.HasFile)
            {
                SaveFile(File_Upload.PostedFile, File_Upload.FileName);
            }
        }
   }


0
Comment
Question by:MedtronicVascSR
  • 2
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 250 total points
ID: 33565399
The problem is the UpdatePanel.
According to Microsoft some controls won't work in an UpdatePanel : one of them is the FileUpload control. (http://msdn.microsoft.com/en-us/library/bb386454.aspx)

Maybe this article is of any help : http://vinayakshrestha.wordpress.com/2008/02/19/aspnet-gridview-multiple-files-upload/
0
 

Author Closing Comment

by:MedtronicVascSR
ID: 33568613
Thanks Albert. Now I can stop chasing my tail on this and start working on a work around :-)
0
 

Author Comment

by:MedtronicVascSR
ID: 33605819
Hi,
I finally found a way to upload files using the FileUpload control on a Gridview within an "update panel". If you create a dummy FileUpload control that is hidden via a div tag on the form you can do it. Even though the function will reference the dummy Panel ID, it will actually pull the correct file control from the grid row (see below). Although, I'm not technically sure how this makes it happen it's possible it follows the same logic that hidden iFrames do for asynchronous file uploads using Ajax.

Keep in mind this was just one part of larger set of data I was submitting so it wasn't simply an file upload task. The task involves attaching files to a training session that could involve any number of employees and documents, which is why I am using grids, etc.

Anyway, wanted to share.

Cheers,
Ty


<div id="divDummy" style="display:none">

   <asp:Panel runat="server" ID="pnlFileUpload" > 

        <asp:FileUpload ID="fupDummy" runat="server"/>

    </asp:Panel>

</div>



<asp:UpdatePanel ID="UpdatePanel5" runat="server" RenderMode="Inline" UpdateMode="Conditional">              <ContentTemplate>

   <%-- Programatically generated Grid--%>

   <asp:gridview ID="gvwFileCart"  runat="server">

     Grid stuff here

   </asp:gridview>

  </ContentTemplate>

</asp:UpdatePanel>







protected void btnSubmitDocumentTraining_Click(object sender, EventArgs e)

{

foreach (GridViewRow rowFileCart in gvwFileCart.Rows)

{

HiddenField tmp_EMPLOYEE_ID = (HiddenField)rowFileCart.FindControl("hfIdEmployeeIDFile");

HiddenField tmp_PLM_DOCUMENTS_ID = (HiddenField)rowFileCart.FindControl("hfIdDocumentIDFile");

FileUpload File_Upload = (FileUpload)rowFileCart.FindControl("upDocumentTrainingEmployeeCartFile");



//Check to see if a file was selected to upload

if (File_Upload != null && File_Upload.HasFile)

{

// Call the function to check to file and save it.

SaveFile(File_Upload.PostedFile, File_Upload.FileName);

}

}

}

Open in new window

0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 33609297
That's both strange and interesting...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now