VIkram, Thanks for the response. You say 'by default' - does that imply that there is some way to make it sort in a non-default way, or do you mean simply that there is no control over the sorting order? I looked at the document you cited, but it isn't clear to me that it gets me any closer - if CheckBoxList always sorts alphabetically by DataTextField then it doesn't really matter what the data source is - once I bind it will sort the way it sorts (?). However, reading your message did cause me to think that maybe I can do this by creating a checkbox template for a column in a datagrid control - because that one lets me control the sort order (if I remember correctly...). I'll go look into that now. Thanks again, Chris
I've created a checkboxlist by binding to a table in a database. The list appears sorted alphabetically by the field I specified as the DataTextField, not the order they occur in the table nor in the order of the specified DataValueField. Is this behavior hard-wired? I don't see any sort-related attributes for checkboxlist controls. I'd like to be able to sort on some other field than the displayed text... I suppose in principle I could add the items one-by-one programmatically, and I suppose (hope) they would then appear in the order in which they were added. I haven't tested this approach yet - seems a pain if there's a more direct method... Thanks for any help/insight. Chris
Yes,CheckBoxList sorts by the DataTextField by default. You may want to look into SortedLists [System.Collections] (See:msconline.maconstate.edu/Tutorials/ASPNET1/ASPNET07/aspnet07-03.aspx) to achieve the sorting you looking for . Vikram DiscountASP.NET www.DiscountASP.NET
Outcome: First, a clarification of my original post with the benefit of hindsight: although I wanted to generate the checkboxlist item descriptions from a data table, I didn't want the actual checkbox statuses (checked vs. unchecked) bound to a set of data values. This only became clear to me when I tried to implement a solution using a gridview - creating a CheckBoxField in a gridview column binds the checkboxes to a yes/no type of data field, and unless editing is enabled it acts like a read-only display of those values. What I wanted was a list of checkboxes that the user could set arbitrarily and without being in edit mode - but I wanted the list of checkbox items to come from a table. The (or, I should say, one) solution (including the sorting problem I originally asked about) is to use a CheckBoxList and populate it on Page_Load by creating a SELECT query with the desired ORDER BY clause, then iterating through the results using a datareader to populate the checkboxlist. Here's the routine I call from the Page_Load handler: Protected Sub InitIssAndRecFilter() Dim con As New SqlConnection(connectionString) Dim sqlSel = 'SELECT LongName FROM keyIssues ORDER BY SortFld ASC' Dim cmdSel As New SqlCommand(sqlSel, con) Dim reader As SqlDataReader con.Open() reader = cmdSel.ExecuteReader() Do While reader.Read() cblIssAndRec.Items.Add(reader('LongName').ToString) Loop reader.Close() con.Close() End Sub Later I can, of course, loop over the CheckBoxList items to see which ones have been selected. Hope this helps someone down the line. Chris
Well, in case it wasn't already obvious I'll confess to being new to ASP.NET... Having climbed a few steps higher on the learning curve, I realize that it's totally straightforward to build a gridview with unbound checkboxes in a column, and it solves the sorting issue too because sorting can be controlled in gridviews. The key is to create a TemplateField for one of the columns, with an ItemTemplate specifying a simple <asp:CheckBox...>. These unbound checkboxes can be examined on postback to see which items were selected. The example I came across, which includes a 'Select All' feature, is here: http://www.codeproject.com/aspnet/SelChkboxesDataGridView.asp Hope this steers others straight if they stumble across this thread... Chris