• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

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
0
ayha1999
Asked:
ayha1999
  • 4
  • 3
1 Solution
 
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
 
brdrokCommented:
gotcha.  what does your sql query look like?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
praneethaCommented:
//before this uncheck everything...
while(reader.Read())
{
 
       this.chkMyCheckBoxList.Items[int32.Parse((string) reader[0])].Selected == true;

  }
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now