Checkboxlist

Hi,

I have checkboxList control in a form with the follwoing data

1,2,3,4,5 and 6 (values and texts)

in a table column I have only 1,3 and 5

How can I set 1,3 and 5 as selected in the checkboxlist in the page load event.

ayha
LVL 7
ayha1999Asked:
Who is Participating?
 
praneethaCommented:
//before this uncheck everything...
while(reader.Read())
{
 
       this.chkMyCheckBoxList.Items[int32.Parse((string) reader[0])].Selected == true;

  }
0
 
brdrokCommented:
have you tried something like the following:

if(!Page.IsPostBack)
{
   this.chkMyCheckBoxList.Items[0].Selected == true;
   this.chkMyCheckBoxList.Items[2].Selected == true;
   this.chkMyCheckBoxList.Items[4].Selected == true;
}

0
 
ayha1999Author Commented:
Hi brdrok,

I have to read data from a table and assign to the chekcboxlist.

ayha
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
brdrokCommented:
gotcha.  what does your sql query look like?
0
 
ayha1999Author Commented:
HI,

select typenos from mytable

ayha
0
 
brdrokCommented:
try something like the following:

SqlConnection cn;
SqlCommand cmd;

string sql = "select typenos from mytable"

cn = new SqlConnection(ConfigurationSettings.AppSettings["YourConnectionString"]);

cmd = new SqlCommand(sql, cn);
cn.Open();

SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())
{
    if( (string) reader[0] = "1")
       this.chkMyCheckBoxList.Items[0].Selected == true;

    if( (string) reader[0] = "2")
       this.chkMyCheckBoxList.Items[1].Selected == true;

   etc.  
}

on second thought..maybe a switch statement would be much more appropriate...but I have a cold and brain is starting to shut down =)
0
 
ayha1999Author Commented:
Hi,

Any other way to do it without mentioning values like;

 if( (string) reader[0] = "1")
       this.chkMyCheckBoxList.Items[0].Selected == true;

all values in the table will be available in the list. I want to select the values accordingly.

ayha
0
 
brdrokCommented:
what is your criteria for selecting whether a checkbox is checked or not?  I thought this was determined via the database.  So, if your query returns

1
3
5

and those will be selected in your checkbox list control, no?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.