Rengaw
Rengaw

Reputation: 149

Having issues with checkboxes

I have two websites with three checkboxes on both of them. The thing i want to do is check a checkbox on the first website and it would show what i have checked on the other site. This works with only one checkbox but as soon as i check more than one box only one shows as checked on the other site. Here is some code:

Website1.aspx (Where they have to be checked)

if (CheckBox1.Checked)
{
    Response.Redirect("default.aspx?parm1=true");       
}
else if (CheckBox2.Checked)
{
    Response.Redirect("default.aspx?parm2=true");
}
else if (CheckBox3.Checked)
{
    Response.Redirect("default.aspx?parm3=true");
}

Website2.aspx (where they should show up as checked)

if (Request.QueryString["parm1"] != null)
{
    boxreg.Checked = Convert.ToBoolean(Request.QueryString["parm1"]);
}
else if (Request.QueryString["parm2"] != null)
{
    boxhand.Checked = Convert.ToBoolean(Request.QueryString["parm2"]);
}
else if (Request.QueryString["parm3"] != null)
{
    boxbeslut.Checked = Convert.ToBoolean(Request.QueryString["parm3"]);
}

So the question is how would i go about if i want more than one checked and for it to show up on the other site. If i check only one for now it works but not for more than one.

Upvotes: 1

Views: 60

Answers (2)

mybirthname
mybirthname

Reputation: 18127

You can do something like this.

First page

string params = "";

params+= CheckBox1.Checked ? "param1=true":"";
params+= CheckBox2.Checked ? "param2=true":"";
params+= CheckBox3.Checked ? "param3=true":"";

string url = "default.aspx"

url += params != "" ? "?" + params: "";
Response.Redirect(url);

Default.aspx

boxreg.Checked = Request.QueryString["parm1"] != null ? true: false;
boxhand.Checked = Request.QueryString["parm2"] != null ? true: false;
boxbeslut.Checked = Request.QueryString["parm3"] != null ? true: false;

Upvotes: 0

Ian
Ian

Reputation: 30813

I suppose, your code should be changed to something like this:

Website1.aspx

Response.Redirect("default.aspx?parm1=" + CheckBox1.Checked.ToString() + 
"&parm2=" + CheckBox2.Checked.ToString() + 
"&parm3=" + CheckBox3.Checked.ToString());

And also (note the three ifs):

Website2.aspx

if (Request.QueryString["parm1"] != null)
{
    boxreg.Checked = Convert.ToBoolean(Request.QueryString["parm1"]);
}

if (Request.QueryString["parm2"] != null)
{
    boxhand.Checked = Convert.ToBoolean(Request.QueryString["parm2"]);
}

if (Request.QueryString["parm3"] != null)
{
    boxbeslut.Checked = Convert.ToBoolean(Request.QueryString["parm3"]);
}

Upvotes: 1

Related Questions