Maintain Session Object after Postback in ASP

I recently developed a small application, using a control “FileUpload” which let me select a file that I could upload to a database. The main problem that I was having was with the PostBack and AutoPostback. In this article I will give you a step-by-step guide on how maintain session object after Postback or Auto PostBack in ASP

Important: The FileUpload control does not automatically save a file to the server 

The FileUpload control does not automatically save a file to the server after the user selects the file to upload. You must explicitly provide a control or mechanism to allow the user to submit the specified file. For example, you can provide a button that the user clicks to upload the file. The code that you write to save the specified file should call the SaveAs method, which saves the contents of a file to a specified path on the server. Typically, the SaveAs method is called in an event-handling method for an event that raises a post back to the server. For example, if you provide a button to submit a file, you could include the code to save the file inside the event-handling method for the click event.

According to the official ASP documentation, before calling the SaveAs method to save the file to the server, you must use the HasFile property to verify that the FileUpload control contains a file, or the path for the file that you want to upload. If the HasFile returns true, the FileUpload calls the SaveAs method. If it returns false, it displays a message to the user indicating that the control does not contain a file. On PostBack/AutoPostBack

Solution

The solution is to use Session State. Session State enables you to store and retrieve values for a user, even if he goes to another page of your application.

Open the file “Page_Load” of your asp application, and then add the following code for maintaining the session:

//If the page is for the first time and have the file control "FileUpload"
//but not in session. the value stored in the session object
if (Session["FileUpload1Object"] == null && FileUploadApp.HasFile)
{
Session["FileUpload1"] = FileUploadApp;
Label1.Text = FileUploadApp.FileName;
}
// It refreshes the page and if you have values, but the "fileupload" is blank
// Return values of the session
else if (Session["FileUpload1Object"] != null && (!FileUploadApp.HasFile))
{
FileUploadApp = (FileUpload)Session["FileUpload1Object"];
Label1.Text = FileUploadApp.FileName;
}
// If the user changes the file
// change the file in the session object
else if (FileUploadApp.HasFile)
{
Session["FileUpload1Object"] = FileUploadApp;
Label1.Text = FileUploadApp.FileName;
}
//Label1 saves the file name
//If the page is for the first time and have the file control "FileUpload"
//but not in session. the value stored in the session object
if (Session["FileUpload1Object"] == null && FileUploadApp.HasFile)
{
Session["FileUpload1"] = FileUploadApp;
Label1.Text = FileUploadApp.FileName;
}
// It refreshes the page and if you have values, but the "fileupload" is blank
// Return values of the session
else if (Session["FileUpload1Object"] != null && (!FileUploadApp.HasFile))
{
FileUploadApp = (FileUpload)Session["FileUpload1Object"];
Label1.Text = FileUploadApp.FileName;
}
// If the user changes the file
// change the file in the session object
else if (FileUploadApp.HasFile)
{
Session["FileUpload1Object"] = FileUploadApp;
Label1.Text = FileUploadApp.FileName;
}
//Label1 saves the file name

Now, open the web.config file, and add the following code inside the <system.webServer> to turn cache off:

<caching>
            <profiles>
                <add extension=".aspx" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="00:00:30" />
            </profiles>
        </caching>

Save the changes and that’s it, your session object will not be deleted.

This Concludes Maintain session object after Postback in ASP

Looking for quality ASP Web Hosting? Look no further than Arvixe Web Hosting!

Happy Hosting!

Rodolfo Hernandez

Tags: , , , , , , , , , , , , , | Posted under ASP .NET 3.5, DotNet/Windows Hosting | RSS 2.0

Author Spotlight

Rodolfo Hernandez

Rodolfo Hernandez

I like photography and reading books. Currently working for Arvixe as Elgg Community Liaison. Elgg Security Expert Web Security Expert CEO of UDP SW Social Web

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + = 7

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>