Solved

Reading FileUpload Control Values From GridView...

Posted on 2010-08-30
4
1,118 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 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

18 Experts available now in Live!

Get 1:1 Help Now