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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

ayha1999Author Commented:
Hi brdrok,

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

ayha
brdrokCommented:
gotcha.  what does your sql query look like?
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

ayha1999Author Commented:
HI,

select typenos from mytable

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

  }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.