1

in my web application i am checking session state like this.. in page load even of one of the page but it is every time session is not null is showing see this is my code...

if (Session["my"] != null)
{
  Response.Write("hi");
}
else
{
  Response.Redirect("default.aspx");
}

this is the page where i am assigning value to the session like this i am taking one button and one link button and write the code like this

protected void btn_Click(object sender, EventArgs e)
{
    Session["my"] = "surya";
    Response.Redirect("default1.aspx");
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Response.Redirect("default1.aspx");
}

but every time it is showing i mean even i click on link button it is showing "hi" message can u help me is there any ispostback problem is there

1
  • 1
    Learn to use punctuation please! Commented Dec 7, 2010 at 14:55

3 Answers 3

1

It is normal because you assign a value to the session variable "my" and in default.aspx you write "hi" if the session variable "my" is not null.

Just change the line

Response.Write("hi");

to

Response.Write(Session["my"].ToString());

and you will see the value of session variable "my".

Sign up to request clarification or add additional context in comments.

Comments

1

What if you add another button and click handler:

protected void Button2_Click(object sender, EventArgs e)
{
   Session.Abandon();
   Response.Redirect("default1.aspx");
}

I expect the session to be cleared then, so that that "Hi" is not printed.

What happened (I guess) is that you put something in the session and never cleared it.

Note: that Session.Abandon() clears the entire session. You could also use Session.Remove("my"); to clear just this value.

Comments

0

Page Load is executed each time the page is requested

Which is why (when your session != null) "Hi" is being printed on the screen. Are you just wanting this to happen once?

If so explore Page.IsPostBack

private void Page_Load()
{
    if (!IsPostBack)
    {
        // Will only happen if its not a postback
        // (first request)
    }
}

5 Comments

@m.edmondson i want to check if session is empty it is redirect the default.aspx.if session contain value "hi" message should display
@Surya - You are redirecting to another page on postback (default1.aspx) are you sure this isn't the page you're seeing "Hi" on?
@m.edmondson i am assigning session value in button and not in link button and redirecting to default1.aspx page where i am checking session if it is empty redirect to default.aspx page if session value is
@Surya - Almost certainly its because you've clicked it already. Its getting assigned a value, so from that point onwards (until the session expires) it will print "Hi". Have you put a breakpoint on to check this?
@Surya - So is it because you have previously assigned "Hi" to the session?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.