locked
Using session variable in a user control (.ascx file) RRS feed

  • Question

  • User-207680675 posted

    Hi there,

    I have received a piece of code in which there is a session variable in a user control file (.ascx file). I have to use this user control at 2 different places in ASP.NET application. 

    Problem is if an user opens these 2 pages concurrently and tries to update values on those pages, there will be mixing of values for session variable. Is there any way to address this issue ?

    I am open to replace session variables if there is any better alternative. I would like to keep the use of session variables exclusive to the page on which its being used. Please let me know.

    Thanks,

    Amit

    Wednesday, September 9, 2015 5:57 PM

Answers

  • User-286291038 posted

    Hi there,

    Even if a user control was not used, the same problem will probably exist if the same session variable was used in different pages and the different pages were opened at the same time concurrently by the same user.

    So, if you want something exclusively for the page, then I do not think session variable is a good option. You can try for a different solution, such as maybe using private variable just in cope of that page. Or a static variable in case the data is shared (but always be very careful while using static variables).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 9, 2015 6:09 PM

All replies

  • User-286291038 posted

    Hi there,

    Even if a user control was not used, the same problem will probably exist if the same session variable was used in different pages and the different pages were opened at the same time concurrently by the same user.

    So, if you want something exclusively for the page, then I do not think session variable is a good option. You can try for a different solution, such as maybe using private variable just in cope of that page. Or a static variable in case the data is shared (but always be very careful while using static variables).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 9, 2015 6:09 PM
  • User-207680675 posted

    Thanks for the response Nataraj !

    Friday, September 11, 2015 2:28 PM