Sara Nikta Yousefi
Sara Nikta Yousefi

Reputation: 1607

check and uncheck a dynamic checkboxlist

I have this checkboxlist that it's items fills with linq datasource now I want to check some of these check boxes programmatically... this is my checkboxlist:

<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="LinqDataSource2" DataTextField="ProjectGroupTitle" DataValueField="ProjectGroupID"></asp:CheckBoxList>

and this is my code trying to check some of these check boxes so far:

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                        {
                            if (CheckBoxList1.Items[i].Text == j.ProjectGroupTitle)
                            {
                                CheckBoxList1.Items[i].Selected = true;
                            }
                        }

when I checked this piece of code in debug mode I realized that CheckBoxList1.Items.Count value is 0 which is odd as I have multiple value in my database that linq datasource is responsible for fetching them for checkboxlist... could someone help me fix this code?

Upvotes: 2

Views: 2540

Answers (2)

Sharique Hussain Ansari
Sharique Hussain Ansari

Reputation: 1456

try to test your code in page render:-

<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="LinqDataSource2" DataTextField="ProjectGroupTitle" DataValueField="ProjectGroupID" OnDataBound="SelectCheckbox"></asp:CheckBoxList> 

 public void SelectCheckbox(object sender, EventArgs e)
        {
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                        {
                            if (CheckBoxList1.Items[i].Text == j.ProjectGroupTitle)
                            {
                                CheckBoxList1.Items[i].Selected = true;
                            }
                        }
    }

Upvotes: 2

Med.Amine.Touil
Med.Amine.Touil

Reputation: 1235

Check the Request.Form variables while debugging. And please provide as with more information. (is it an aspx or an ascx code behind. is there any update Panel ...)

Upvotes: 3

Related Questions