Checkbox c#

Don
Joined
5 Oct 2005
Posts
11,239
Location
Liverpool
Hey All,

I'm trying to get a checkbox to automatically be checked depending on something but its not working, my code is:

if (Avail == "Yes")
{
CheckBoxAvail.Checked = true;
}
else
{
CheckBoxAvail.Checked = false;
}

any ideas why this is not working??

Stelly
 
Stelly said:
Hey All,

I'm trying to get a checkbox to automatically be checked depending on something but its not working, my code is:

if (Avail == "Yes")
{
CheckBoxAvail.Checked = true;
}
else
{
CheckBoxAvail.Checked = false;
}

any ideas why this is not working??

Stelly

Does Avail definitely contain what you think it should contain, and in the correct case? Have a look in the debugger first of all the make sure that Avail definitely contains "Yes" when you expect it to.

You're much better off doing this sort of comparison with a boolean type or at the very least an integer type.
 
Your code is correct. I am going to assume Avail is a string, I would make Avail a bool or int as said previously.

How are you assigning a value to Avail?

TrUz
 
string Avail = "Yes";

Response.Write(Avail);

if (Avail == "Yes")
{
CheckBoxAvail.Checked = true;
}
else
{
CheckBoxAvail.Checked = false;
}

That works fine... but this does not...

string Avail = Comp["Available"].ToString();

Response.Write(Avail);

if (Avail == "Yes")
{
CheckBoxAvail.Checked = true;
}
else
{
CheckBoxAvail.Checked = false;
}
 
whole code...
protected void Page_Load(object sender, EventArgs e)
{
int PCID = Convert.ToInt32(Request.QueryString["pcid"]);

DataSet CompSet = SQLQueries.CompDetailBindData(PCID);

string sPCIDValue = Convert.ToString(PCID);

if (!IsPostBack)
{


if (null != CompSet && 0 < CompSet.Tables[0].Rows.Count)
{
DataRow Comp = CompSet.Tables[0].Rows[0];

LabelCompModel.Text = Comp["PcModel"].ToString();
TextBoxCCCSR.Text = Comp["CCCSR"].ToString();
TextBoxEPSR.Text = Comp["EPSR"].ToString();
TextBoxShipDate.Text = Comp["ShipDate"].ToString();
LabelOnsite.Text = Comp["OnSite"].ToString();
LabelUser.Text = Comp["UserID"].ToString();

string Avail = Comp["Available"].ToString();

//string Avail = "Yes";

Response.Write(Avail);

if (Avail == "Yes")
{
CheckBoxAvail.Checked = true;
}
else
{
CheckBoxAvail.Checked = false;
}


}
}

}


Stelly
 
Why not use a boolean type for "Avail" instead of "Yes" and "No" ?


then instead of:
string Avail = Comp["Available"].ToString();

if (Avail == "Yes")
{
CheckBoxAvail.Checked = true;
}
else
{
CheckBoxAvail.Checked = false;
}

you can do this:

CheckBoxAvail.Checked = Comp["Available"]
 
Yup, a much better idea although at it looks like its coming back from a db it'll probably need converting via Convert.ToBoolean() or ToBool() as I cant remember off the top of my head.
 
roboffer said:
Yup, a much better idea although at it looks like its coming back from a db it'll probably need converting via Convert.ToBoolean() or ToBool() as I cant remember off the top of my head.

If the field is defined as a boolean in the DB, it won't need converting. If it isn't, you'll have to convert it from a string to a bool:

myCheckbox.checked = bool.Parse(value);
 
Back
Top Bottom