Monday, October 13, 2014

How to Maintain State of FileUpload Control in Asp.Net

Generally when we upload any file using FileUpload Control and if their is TextBox with TextChange Event ,When TextChange Event fires the file from FileUpload  Control will looses file due to postback,, on PostBack PostedFile property gets initilized with HttpPostedFile object for the file. As we know that http request can't maintain state, and PostedFile is initilized with HttpPostedFile Object so it loose it's state.

By using State Managment Techniques we maintain File in FileUpload Control.

Lets Design one Page with FileUpload Control,Label, TextBox and Button.



In Example.aspx

Add code in form tag

<div>
    <table>
        <tr>
            <td>
                Enter Name :
            </td>
            <td>
                <asp:TextBox runat="server" ID="txtname" OnTextChanged="txtname_TextChanged"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Upload File :
            </td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                <asp:Label runat="server" ID="lblFilename" Visible="false" ></asp:Label>
             
            </td>

        </tr>
        <tr>
            <td colspan="2">
                <asp:Button runat="server" ID="btnSubmit" />
            </td>
        </tr>
          </table>
    </div>

After Adding following output will be seen













First Upload file using FileUpload Control ,then enter any text in textbox when you come out from textbox the fileupload control looses file because postback occurs as we kept OnTextChanged Event for textbox so its postbacks .So to maintain uploaded file we use session object because session can store any type of object.

So add following code in Example.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FileUploadExample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //If first time page is submitted and we have file in FileUpload control but not in session
        // Store the values to SEssion Object
        if (Session["FileUpload1"] == null && FileUpload1.HasFile)
        {
            Session["FileUpload1"] = FileUpload1;
            lblFilename.Visible = true;

            lblFilename.Text = FileUpload1.FileName;
        }
        // Next time submit and Session has values but FileUpload is Blank
        // Return the values from session to FileUpload
        else if (Session["FileUpload1"] != null && (!FileUpload1.HasFile))
        {
            FileUpload1 = (FileUpload)Session["FileUpload1"];
            lblFilename.Visible = true;
            lblFilename.Text = FileUpload1.FileName;
        }
        // Now there could be another sictution when Session has File but user want to change the file
        // In this case we have to change the file in session object
        else if (FileUpload1.HasFile)
        {
            Session["FileUpload1"] = FileUpload1;
            lblFilename.Visible = true;
            lblFilename.Text = FileUpload1.FileName;
        }
    }
    protected void txtname_TextChanged(object sender, EventArgs e)
    {
     
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Here you Can access fileupload control properties for inserting onto folder
        Session.Remove("FileUpload1");
        }
}

Here i took Lable Control to show that file is there to the user so that hw knows file is there.
Finially Execute application you will see folllowing output





No comments:

Post a Comment