0

I am trying to upload files from the client to server through asp.net. The problem is that the fileupload control doesn't work inside an TabPanel. Basically the file name and postedfiles properties of fileupload are null after submit. And I can't kick TabPanel out.

I am looking for other solutions. One is that I require the user to input the path of the the files into an input field and submit a button.

The question is how to upload a file based on the path of the file?

Your help will be highly appreciated.

The code is like this. The control id in question is "fuAttachments"

<asp:updatepanel id="upnlForTab" runat="server">
<ContentTemplate>
            <asp:TabContainer ID="tcFS" runat="server" ActiveTabIndex="0">                    
                <asp:TabPanel ID="TabPanelAnnualFS" runat="server" HeaderText="AnnualFS">                    
                    <ContentTemplate>     
                        <asp:FormView ID="fvAnnualFS" OnDataBound="fvAnnualFS_DataBound" runat="server">
                            <InsertItemTemplate>
                            </InsertItemTemplate>
                            <EditItemTemplate>
                                <table>
                                    <tr>
                                        <td colspan="2">
                                            Edit a Financial Statement
                                            <asp:HiddenField ID="hfIDStatement" runat="server" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Attachments:
                                        </td>
                                        <td>
                                            <asp:GridView ID="gvAttachments" AutoGenerateColumns="false" runat="server">
                                                <Columns>
                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" Text='<%#Eval("AttachmentName")%>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                </Columns>
                                            </asp:GridView>
                                            <asp:FileUpload ID="fuAttachments" runat="server" /> 
                                            <asp:Button ID="btnAddAttachment" runat="server" OnClick="btnAddAttachment_Click" Text="Add" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Button ID="btnSubmitEditAnnualFS" ValidationGroup="SaveFS" OnClick="btnSubmitEditNewAnnualFS_Click"
                                                runat="server" Text="Submit" />
                                        </td>
                                        <td>
                                        </td>
                                    </tr>
                                </table>
                            </EditItemTemplate>
                        </asp:FormView>
                    </ContentTemplate>
                </asp:TabPanel>
            </asp:TabContainer>
</ContentTemplate>

in the backend, the code is like this. The problem is that all the properties for fuAttachments are not posted, which is a known issue for file upload control within updatepanel and tabpanel.

        /// <summary>
    /// Add document to the document list.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddAttachment_Click(object sender, EventArgs e)
    {
        HiddenField hfIDStatement = fvAnnualFS.FindControl("hfIDStatement") as HiddenField;
        FileUpload fuAttachments = fvAnnualFS.FindControl("fuAttachments") as FileUpload;
    }
Ike
  • 1,194
  • 12
  • 18

1 Answers1

1

First off, your HiddenField and FileUpload will not be found the way you are lookng for them. They are nested within other objects, so you will have to find their parent objects first, a bit of a pain, I know.

    protected void btnAddAttachment_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        TemplateControl UploadContainer = (TemplateControl)btn.Parent;

        HiddenField hfIDStatement = (HiddenField)UploadContainer.FindControl("hfIDStatement");
        FileUpload fuAttachments = (FileUpload)UploadContainer.FindControl("fuAttachments");
    } 
AntLaC
  • 1,215
  • 10
  • 22
  • I am sorry. But your way is not working after a try. I get error in the line TemplateControl UploadContainer = (TemplateControl)btn.Parent; because the btn.Parent gets the table cell in which the button is located. As a matter of fact, my code for the hiddenfiled does work. I can get the value of the hidden field. – Ike Oct 16 '12 at 21:06
  • the error message is like this. Unable to cast object of type 'System.Web.UI.WebControls.TableCell' to type 'System.Web.UI.TemplateControl'. – Ike Oct 16 '12 at 21:11
  • ya, sorry about that, I can't get the tabcontrol on my page so I can test. I'm using VS2012 – AntLaC Oct 16 '12 at 21:20
  • Thank you all the same. Can we turn to this question ? The question is how to upload a file based on the path of the file? The idea is that I will ask the user to manually enter the path of the file and submit so that I can get the path of the file on the server side. But I don't know how to get the file from this path. – Ike Oct 17 '12 at 16:04
  • Are you not using the file upload component? If you are, the use will browse to where their file is and upload it. You have to specify where on the server you will place the file, here is a good tutorial on how to use it: http://asp.net-tutorials.com/controls/file-upload-control/ – AntLaC Oct 17 '12 at 16:44
  • No, my intention is to get rid of FileUpload because it doesn't work inside an updatepanel. So I am wondering if I can upload a file without a fileupload control. Maybe I will just ask the user to enter the path of the file in a textbox. Then I will do something in the server side with that path to load the file. – Ike Oct 17 '12 at 20:04
  • The server will not be able to read from the user's computer, that is a HUGE security issue. How the file upload component works is by sending the file through the request stream and the server reads that – AntLaC Oct 18 '12 at 19:23
  • Thanks. I ended up using a different page without updatepanel to upload the documents. – Ike Oct 19 '12 at 16:02