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>");
}
}
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>