Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends,
i have a one CheckboxList control like below:

ASP.NET
<asp:CheckBoxList ID="cblGA">
<asp:ListItem Text="African American/African Heritage" Value="0" >
<asp:ListItem Text="African American/African Heritage" Value="0" >
                 <asp:ListItem Text="Asian-Central/South Asian Heritage" Value="1">
                 <asp:ListItem Text="Asian-Japenese Heritage" Value="2">
                 <asp:ListItem Text="Native Hawaiian or Other Pacific Islander" Value="3">
                 <asp:ListItem Text="White-White/Caucasian/European Heritage" Value="4">
               
                <asp:ListItem Text="American Indian or Alaskan Native" Value="5">
                 <asp:ListItem Text="Asian-East Asian Heritage" Value="6">
                 <asp:ListItem Text="Asian-South East Asian Heritage" Value="7">
                 <asp:ListItem Text="White-Arabic/North African Heritage" Value="8">
                  <asp:ListItem Text="Other" Value="9" />



i want to get the values from the checkbox list even if some are not selected and save it in Database.
how to do it?

pl reply
Posted
Updated 10-Jul-12 1:12am
v2

XML
<asp:CheckBoxList  runat ="server"    ID="cblGA">
<asp:ListItem Value="A" >AA</asp:ListItem>
<asp:ListItem Value="B">BB</asp:ListItem>
<asp:ListItem Value="C">CC</asp:ListItem>
</asp:CheckBoxList>

and write on code behind

 
          <pre lang="c#">  string chkboxlistValue = "";
            foreach (ListItem val in cblGA.Items)
            {
                if (val.Selected)
                {
                    chkboxlistValue += val.Value + " ";
                }
            }






now you can use variable chkboxlistValue To save in database
 
Share this answer
 
v4
C#
string CheckedId = "";
string UncheckedId = "";
foreach (ListItem lst in cblGA.Items)
{
  if (lst.Selected)
  {
     CheckedId += Convert.ToSingle(lst.Value) + ",";
  }
  else
  {
     UncheckedId += lst.Value + ",";
  }
}
if (CheckedId.EndsWith(","))
{
  CheckedId = CheckedId.Remove(CheckedId.Length -1, 1);
}
if (UncheckedId.EndsWith(","))
{
  UncheckedId = UncheckedId.Remove(UncheckedId.Length -1, 1);
}


Here CheckedId Contain Checked Value and UncheckedId contain Unchecked value so u can use it with check and unchecked.
 
Share this answer
 
v2
C#
foreach (ListItem li in cblGA.Items)
        {
            if(li.Selected == false)
             {
            // Code to save in database 
              }
        }


OR
I you are having fix sequence in checkboxlixt then you can directly use index to get value while saving to database
String strListItem = CheckBoxList1.Items[0].Text;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900