views:

33

answers:

3

I have a two checkboxes on different pages. I am sending the value from the first checkbox using session like this:

protected void Button4_Click(object sender, EventArgs e)
        {
            Session["VSSsnap"] = CheckBox1.Checked;
            Response.Redirect("~/Addnewpolicy4.aspx");
        }

I receive this session like this on the next page:

string vss = Session["VSSsnap"].ToString();

However, I want to put this value in a checkbox on the second page.

I tried this, but I get an error:

CheckBox1.Checked = Session["VSSsnap"].ToString();

I also tried this; when I debug, the values are also present (but not displayed by checkbox):

CheckBox1.Checked.Equals(Session["VSSsnap"]);

Any help would be greatly appreciated.

+2  A: 

Use below code :

if( Session["VSSsnap"] != null )
{
 CheckBox1.Checked = Convert.ToBoolean(Session["VSSsnap"]);
}
Mahin
Michael or Luke's code is recommended compared to mine.
Mahin
thanks... that worked...
The code will work, but I think Michael or Luke's code is recommended compared to mine.
Mahin
+1  A: 

The Checked property of the checkbox is a bool, not a string.

You're trying to assign a string to the Checked property which is why you're getting an error.

Try this instead:

CheckBox1.Checked = (bool)(Session["VSSsnap"] ?? false);
LukeH
+1 becuse your snippet is a recommended one than mine.
Mahin
great minds think alike
Michael Edwards
+2  A: 

You are not casting the value from the Session. Try:

CheckBox1.Checked =  (bool) (Session["VSSsnap"] ?? false);

The ?? check to ensure that if VSSsnap is null for any reason false will be returned.

Michael Edwards
+1 because your snippet is a recommended one than mine.
Mahin