Hi i have some doubt regarding check box list
i have a check box list
<asp:CheckBoxList ID="emailcheklist" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Value="1">aasp:ListItem> <asp:ListItem Value="2">basp:ListItem> <asp:ListItem Value="3">casp:ListItem> <asp:ListItem Value="4">dasp:ListItem> asp:CheckBoxList>
and while saving to database am taking 2 power of value.if multiple options are selected it will add up.
supose if i select a nd b
it will 2 power 1+2 power 2 which will be
6.
but while retrieving how can i check the items a nd b.
anybod knows bit operator to check this?
pls help
Thank u
Namitha
NamithaPosted Jun 1, 2009, 11:58 PM
nyway thanks for your suggestion.
I saved the value as 2 power and i got a way to retrieve.I took the binray equivalent of the value and the number of 1's in binary will gve my options selected.
Kirtan PatelPosted Jun 1, 2009, 11:57 PM
Niradhip ChakrabortyPosted Jun 1, 2009, 5:17 PM
You can store the selected checkbox value with comma separator. Now say someone select first, second and fourth checkbox. So it will be store as 1,2,4 in database.
While retrieving from database split the comma separated value and checked it like below.
Array arrOptions;
bool blnAvail = false;
int intUserId = Convert.ToInt64(sdrData["usr_int_id"].ToString());
//fetching the comma separated value from database
arrOptions = sdrData["usr_vch_Rights"].ToString().Split(',');
// assign the comma separated value in array
emailcheklist.Items.Clear();
for (int intIndex = 1; intIndex <= 4; intIndex++)
{
blnAvail = false;
for (int intInnerIndex = 0; intInnerIndex < arrOptions.Length; intInnerIndex++)
{
if (intIndex == Convert.ToInt32(arrOptions.GetValue(intInnerIndex)))
{
blnAvail = true;
break;
}
}
switch (intIndex)
{
case 1:
if (blnAvail)
emailcheklist.Items.Add("a", true);
else
emailcheklist.Items.Add("a");
break;
case 2:
if (blnAvail)
emailcheklist.Items.Add("b", true);
else
emailcheklist.Items.Add("b");
break;
case 3:
if (blnAvail)
emailcheklist.Items.Add("c", true);
else
emailcheklist.Items.Add("c");
break;
case 4:
if (blnAvail)
emailcheklist.Items.Add("d", true);
else
emailcheklist.Items.Add("d");
break;
}
}
Here the concept is we are clearing the checklist box and add it once again based o selected value from database. Try to add the value and item for checklist box in page load from page behind instead of hard coding it in aspx page.
NamithaPosted May 30, 2009, 1:16 AM
Ok that is fine.I too did the same.
And saved the value to database.
But While retrieving I want to show what are the options selected by the user.
How do i do that?
Kirtan PatelPosted May 29, 2009, 7:07 AM
{
int total = 0;
for (int i = 0; i <= CheckBoxList1.Items.Count-1; i++)
{
if (CheckBoxList1.Items[i].Selected == true)
{
total += Convert.ToInt32(CheckBoxList1.Items[i].Value);
}
}
int finalans = total * total;
Label1.Text = finalans.ToString();
}