Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading FileUpload Control Values From GridView...

Posted on 2010-08-30
4
Medium Priority
?
1,197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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