Call hidden FileUpload control on click event

Nuke picture Nuke · Apr 27, 2015 · Viewed 15.7k times · Source

I have a FileUpload control on my asp.net page which is hidden

<asp:FileUpload id="FileUploadControl" runat="server" CssClass="hidden" />

I want this control to be called when user clicks the Browse button

<asp:Button runat="server" id="BrowseButton" text="browse" />

Is it possible to give control to the FileUpload control everytime user clicks the browse button?

EDITED:

On BrowseButton_Click event I want to get upload the file

 protected void BrowseButton_Click(object sender, EventArgs e)
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                    Response.Write("<script>alert('done');</script>");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
            else
            {
                Response.Write("<script>alert('please choose');</script>");
            }
        }

Answer

Rahul Nikate picture Rahul Nikate · Apr 27, 2015

Yes you can do this at client side using jQuery. Please find below code:

<asp:FileUpload id="FileUploadControl" runat="server"  CssClass="hidden" />

<asp:Button runat="server" id="BrowseButton" text="browse" OnClientClick="openfileDialog();"/>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
   function openfileDialog() {
       $("#FileUploadControl").click();
   }
</script>