0

I didn't know exactly how to put the title since i couldn't even describe the problem,, here's the problem..

(This is just the problem in brief)

Say I Have two CheckBoxes Set To AutoPostBack=True

protected void cbPop_CheckedChanged(object sender, EventArgs e)
{
    Response.Cookies["UserPreferences"].Value = Request.Cookies["UserPreferences"].Value + "1";
    Label1.Text = Request.Cookies["UserPreferences"].Value.Length.ToString();
}

protected void cbDown_CheckedChanged(object sender, EventArgs e)
{
    Response.Cookies["UserPreferences"].Value = Request.Cookies["UserPreferences"].Value + "2";
    Label1.Text = Request.Cookies["UserPreferences"].Value.Length.ToString();
}

Here's What I expect :

I expect the label's text to show "1" when i check the first checkBox and to show "2" when i check the second checkBox

Here's What I get :

I get "1" both times, when i manually check the cookie, i see it contains 12 so it's length should become 2 after checking the second checkBox, instead the label shows that it's length remains 1

WHY IS THAT !! Am i missing something ??

2 Answers2

1

If you want to see the value right after CheckedChanged is called, you need to assign Response value to Label.

Otherwise, Label control won't reflect the changes until next time post back.

See the arrows in the comment area -

protected void cbPop_CheckedChanged(object sender, EventArgs e)
{
    Response.Cookies["UserPreferences"].Value = 
       Request.Cookies["UserPreferences"].Value + "1";
    Label1.Text = Response.Cookies["UserPreferences"].Value.Length.ToString();
    //                ^
    //                |
}

protected void cbDown_CheckedChanged(object sender, EventArgs e)
{
    Response.Cookies["UserPreferences"].Value = 
        Request.Cookies["UserPreferences"].Value + "2";
    Label1.Text = Response.Cookies["UserPreferences"].Value.Length.ToString();
    //                ^
    //                |
}
Win
  • 61,100
  • 13
  • 102
  • 181
1

Read Response.Cookies["UserPreferences"].Value's lenth to label. Not the Request's

venu
  • 147
  • 3
  • 15