0

In my application i am using one Fileupload controller ,one dropdown and One button, Here first i am selecting one .doc file using fileupload controller, then i am selecting Dropdown value, when i am clicking button, it checks dropdown value is > 0 or not,

 if (ddlstype.SelectedValue != "0")

if the ddlstype value is equals 0, then it shows an Error message in label. Here the dropdown have AutoPostBack,code follows,

<asp:DropDownList ID="ddlstype" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlstype_SelectedIndexChanged" Width="165px"> 

Here my problem is if the page is AutoPostBack, then the file upload control become null, how can i maintain the file in fileupload controler while AutoPostBack?

1 Answers1

0

Here you store file path in session on page load. This way you not need to use update panel

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

        }
    }
}

And get it on ddlstype_SelectedIndexChanged

protected void ddlstype_SelectedIndexChanged(object sender, EventArgs e)
{
    HttpPostedFile FileUpload1 = Request.Files["FileUpload1"];

    Session["FileUpload1"] = FileUpload1;
}
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76