Solved

Reading FileUpload Control Values From GridView...

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parse string in c# 5 41
How does this modal work? 3 34
Intermittent OleDbConnection Error 20 54
How to send the email using C# with embedded HTML TABLE 3 25
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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