Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

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
Avatar of brdrok
brdrok

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;
}

Avatar of ayha1999

ASKER

Hi brdrok,

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

ayha
gotcha.  what does your sql query look like?
HI,

select typenos from mytable

ayha
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 =)
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
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?
ASKER CERTIFIED SOLUTION
Avatar of praneetha
praneetha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial