Nuke Nuke - 4 months ago 73
ASP.NET (C#) Question

Call hidden FileUpload control on click event

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

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>
Comments