0

I have a listbox and button on the page 1 and when i click on the button, the page 2 will open in a new tab. In the page 2, I'm uploading photos to a folder and set the session["FileName"] value. I want when i close the page 2, names of Uploaded images are displayed in listbox.

Note: session["FileName"] = names of uploaded images.

Does anyone have an idea? Please Help me.

Thank you.

my Upload class:

public void ProcessRequest(HttpContext context)
{      
    if (context.Request.Files.Count > 0)
    {
        // get the applications path 

        string uploadPath = context.Server.MapPath(context.Request.ApplicationPath + "/Temp");
        for (int j = 0; j <= context.Request.Files.Count - 1; j++)
        {
            // loop through all the uploaded files 
            // get the current file 
            HttpPostedFile uploadFile = context.Request.Files[j];

            // if there was a file uploded 
            if (uploadFile.ContentLength > 0)
            {
                context.Session["FileName"] = context.Session["FileName"].ToString() + uploadFile.FileName+",";
                uploadFile.SaveAs(Path.Combine(uploadPath, uploadFile.FileName));
            }
        }
    }
    // Used as a fix for a bug in mac flash player that makes the 
    // onComplete event not fire 
    HttpContext.Current.Response.Write(" ");
}
Morteza Karimi
  • 107
  • 1
  • 18

1 Answers1

0

Session is a server object in ASP.NET. That means, when you create a Session on one page, you can use it on any other page, as long as the Session object wasn't Removed or expired.

Suppose you did this on page1.aspx.cs

Session["FileName"] = "file1";

then you can access it on page2.aspx.cs like this:

if(Session["FileName"]!=null)
    Label1.Text = (string)Session["FileName"]

So that way you can access Session variables only on the .aspx page or Control-derived class.

If you want to access Session variables in a Class Library Project than, do this:

HttpContext.Current.Session["FileName"]

also, it looks like you have created a Custom HttpModule.

be notified that your HTTPModule must not be dealing with any pipeline events that that occur before the session state being initialized.

Read this to know more on how and when to access Session variables in a HttpModule

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
  • Dear Manish Mishra. Thank for help but i have two pages. page 2 will open in a new tab and in the page 2, I'll upload photos and when i close the page 2, i want the session value to be shown in the listbox of page 1. Please help. – Morteza Karimi Apr 20 '13 at 10:27
  • @MortezaKarimi in that case, you will have to refresh your page1. only then it will know about the new session variable created. instead of opening a new tab, open a popup or modeldialog. that way it will be much easier – Manish Mishra Apr 20 '13 at 10:30