Session changing when Response.Redirect in UploadCompleted is fired

  •  05-12-2010, 11:04 AM

    Session changing when Response.Redirect in UploadCompleted is fired

    Hi there,
     
    In my Global.asax I am creating a session under session_start to initialise Session["Session_Id"] but for this example it is being initialised in the Page_Load.
     
    When a file has been uploaded, the UploadCompleted method is fired which then attempts to refresh the page. It's at this point that the Session "Session_Id" changes. Strangely, this only happens on the first attempt. Any attempts made after this will work and the session doesn't change. I don't want the Sessions value to change.
     
    I have attached a .net page called UploadTestPage.aspx which will demonstrate this.
     
    Another thing to mention is that this works fine on my Local machine but when put into a live environment it doesn't work. The live environment is using II6 and has been upgraded to .NET 4. My local environment is using Visual Studio 2010.
     
    Any help is appreciated! 
     
     ------------------------------------------------------------------------------
     
    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">

        void Page_Load(object sender, EventArgs e)
        {
            // Initialise the session if empty.
            if (Session["Session_Id"] == null)
            {
                Session["Session_Id"] = System.Guid.NewGuid();
            }

            // Display the session.
            Label1.Text = Session["Session_Id"].ToString();
        }

        void Uploader1_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
        {
            // code to insert to database.
            // Taken out for this example.
        }

        void Uploader1_UploadCompleted(object sender, CuteWebUI.UploaderEventArgs[] args)
        {
            // When all files have been transfered, refresh the page.
            // It's at this point that the session id changes.
            Response.Redirect(Request.Url.ToString());
        }

        void Button1_Click(object sender, EventArgs e)
        {
            // Change the session.
            Session["Session_Id"] = Guid.NewGuid();
            // Refresh the page.
            Response.Redirect(Request.Url.ToString(), false);
        }
        
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <h1>
            Upload test</h1>
        <CuteWebUI:Uploader ID="Uploader1" InsertText="Upload your images" MultipleFilesUpload="true"
            MaxFilesLimit="10" OnFileUploaded="Uploader1_FileUploaded" ShowProgressInfo="false"
            AutoUseSystemTempFolder="true" ValidateOption-AllowedFileExtensions="jpg,png,gif,bmp"
            runat="server" OnUploadCompleted="Uploader1_UploadCompleted">
        </CuteWebUI:Uploader>
        <div style="border: 1px solid #000;">
            <asp:Button ID="Button1" runat="server" Text="Change session" OnClick="Button1_Click" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>

     
    Filed under: ,
View Complete Thread