Solved

Reading FileUpload Control Values From GridView...

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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