With Update panel it will not be possible to get the Maintain the FileUpload control.
Without using Update Panel.
HTML
<input id="FileUpload1" name="FileUpload1" type="file" />
<br />
<asp:Label ID="ImageErrorLabel" runat="server" />
<asp:Button ID="Button1" Text="Upload" OnClick="Upload" runat="server" />
<asp:DropDownList ID="ddlTexts" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Text="text1" />
<asp:ListItem Text="text2" />
<asp:ListItem Text="text2" />
</asp:DropDownList>
Namespace
using System.IO;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
if (Session["FileUpload1"] == null && Request.Files["FileUpload1"].ContentLength > 0)
{
Session["FileUpload1"] = Request.Files["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(Request.Files["FileUpload1"].FileName);
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
}
else if (Session["FileUpload1"] != null && (Request.Files["FileUpload1"].ContentLength == 0))
{
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(file.FileName);
}
else if (Request.Files["FileUpload1"].ContentLength > 0)
{
Session["FileUpload1"] = Request.Files["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(Request.Files["FileUpload1"].FileName);
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
HttpPostedFile FileUpload1 = Request.Files["FileUpload1"];
Session["FileUpload1"] = FileUpload1;
}
protected void Upload(object sender, EventArgs e)
{
if (Session["FileUpload1"] == null && Request.Files["FileUpload1"].ContentLength > 0)
{
Session["FileUpload1"] = Request.Files["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(Request.Files["FileUpload1"].FileName);
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
SaveFile(file);
}
else if (Session["FileUpload1"] != null && (Request.Files["FileUpload1"].ContentLength == 0))
{
HttpPostedFile file = (HttpPostedFile)Session["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(file.FileName);
SaveFile(file);
}
else if (Request.Files["FileUpload1"].ContentLength > 0)
{
Session["FileUpload1"] = Request.Files["FileUpload1"];
ImageErrorLabel.Text = Path.GetFileName(Request.Files["FileUpload1"].FileName);
SaveFile(Request.Files["FileUpload1"]);
}
}
private void SaveFile(HttpPostedFile file)
{
string fname = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("~/images/" + fname));
ImageErrorLabel.Text = string.Empty;
}