[RESOLVED]Help with Session variable

I have a Session["message] variable from the sign-up page on my project.. IF the user successfully completes the process, he get a message welcoming him to the application right on the next page.

This has been validated and its working…I even add a javascript to slideup the div responsible for that.. 

Problem is that…if i refresh the homepage (redirected page)..the Session["message"] still pops up/slide down.. I want to fix this to only show the message once and expire throughout the time span of the current user_session, till a new session is created..

P.S: I have tried adding this to my index page, but its not working still..

     HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoServerCaching();
HttpContext.Current.Response.Cache.SetNoStore();


Thanks

Maybe you can delete the message right after you show up the message.

how..? that could work.. how do i deploy that.. heres the line of code

  @{if(!string.IsNullOrEmpty((string)Session["message"])){
 <div class="col-md-12 col-sm-12" style="margin-top: -48px; text-align: center">
           <div class="alert alert-success page-alert">
        <strong>@Session["message"]</strong>
    </div>
      </div>}}
@if(Session["message"] != null){
    <div class="col-md-12 col-sm-12" style="margin-top: -48px; text-align: center">
        <div class="alert alert-success page-alert">
            <strong>@Session["message"]</strong>
        </div>
    </div>
    Session["message"] = null;
}

There are a few ways that you could handle this, however the easiest would be to likely clear out the Session variable explicitly after it was used :

@if(!String.IsNullOrEmpty(Session["message"]){
    <div class="col-md-12 col-sm-12" style="margin-top: -48px; text-align: center">
        <div class="alert alert-success page-alert">
            <strong>@Session["message"]</strong>
            <!-- Explicitly clear out your Session value -->
            @{ Session.Remove("message") }
        </div>
    </div>
}

This should prevent any subsequent requests from being able to access the Session, even by refreshing, as it will no longer exist. If this approach doesn’t work, you might consider performing an AJAX call when the page is loaded to handle clearing out the
Session as well (although the above approach should be sufficient). 

Rion Williams

@if(!String.IsNullOrEmpty(Session["message"]){

That isn’t going to compile.

Ah I forgot the necessary cast a string :

@if(!String.IsNullOrEmpty(Session["message"] as string))
{
        <!-- Omitted for brevity -->
}

However the code that you provided by simply checking for the Session should be sufficient (ala your response above). I was apparently typing it up as your answer was initially posted above.

Leave a Reply