Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
for (int i = 0; i < dt.Rows.Count; i++)
{
    foreach(CheckBox chk in Chklst.Items)
    {
        if (chk.Text.IndexOf(dt.Rows[0][0].ToString()) > 0)
        {
            chk.Checked = true;
        }
        else
        {
            chk.Checked = false;
        }
    }
}
i am getting problem here

i have checkboxlist in this 4 list items.
i am checkin with db if value is already in db it get checked else unchecked..

here i took temp cheeck box chk. how to compare temp checkBox with cheklistbox

the error what i am gettin here is
Unable to cast object of type 'System.Web.UI.WebControls.ListItem' to type 'System.Web.UI.WebControls.CheckBox'.

how to do ????plz help me
Posted
Updated 20-Oct-11 22:00pm
v2

I think here ur checking for the indexof the checklistitem(instead of verifying the value which is stored in a table)

just compare the value of the checklist and the value in the datatable
 
Share this answer
 
You generate a set of integer indexes in the variable 'i in the outer for loop, but you never use the variable 'i in the loop.

"if (chk.Text.IndexOf(dt.Rows[0][0].ToString()) > 0)"

IndexOf returns an integer ... then you convert that integer to a string, and then you compare it to an integer ... that should give you a compile error right there.
 
Share this answer
 
v2

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