I'm having a problem dynamically populating a checkboxlist control. The checkboxlist control is simple: <asp:CheckBoxList id="thisCompanyList" runat="server" /> And here's the VB code to populate it from a database loop: thisCompanyList.Items.Add(new ListItem(myReader("CompanyName").ToString(), myReader("CompanyID").ToString())) I was under the impression this is the method to populate a checkbox (or radio) list - did this change in .NET 2.0 that I'm not aware of? control.Items.Add(new ListItem("Text", "Value")) The problem I am having is my code populates both the text and value with whatever text I put first, so I know it's not a SQL select problem. If I list the company Name first, both the value and text are the company Name. If I list the company ID, then both the value and text for the checkboxes are the ID. So I am properly pulling the values from the database. The problem is with the syntax I am using to populate the text and value fields for the checkbox. Any help is appreciated, tia~!
You should bind the checkbox list to a datareader like following ' assuming that GetListItems returns a list of comanyname and companyid items in a satareader object Dim myReader As SqlDataReader = GetListItem() thisCompanyList.DataSource = myReader thisCompanyList.DataValueField = "CompanyID" thisCompanyList.DataTextField = "CompanyName" thisCompanyList.DataBind() Code (markup): HTH Regards
yugolancer; thanks for that, that's another way to do it and it works perfectly. I did solve my particular problem -- it was (of course) just a bug in my code elsewhere. On parsing the submitted results of the checkboxlist I was pulling thisCompanyList.Items(i).Text instead of thisCompanyList.Items(i).Value. Thanks again!