473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Cannot see Checkboxlist

I have several dynamically populated checkboxlists by objectdatasources. This works well. When I try to count the number of items in each of the checkboxlists it tells me the count is 0. Even though I can see that there are 11 items in in the list.
I can't tell why it is not seeing the items - especially when I'm not getting coding errors.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <asp:CheckBoxList ID="level1Authorizations" runat="server" 
  3.     DataSourceID="Level1AuthDS" DataTextField="AuthDef" DataValueField="AuthID"
  4.      RepeatDirection="Horizontal"  RepeatColumns="2" Width="100%" >
  5.     </asp:CheckBoxList>
  6.  
  7. int Count1 = level1Authorizations.Items.Count;
  8. Response.Write(" level1Authorizations.Items.Count " + Count1);
  9.  
  10.  
Jun 2 '09 #1
7 3532
tlhintoq
3,525 Expert 2GB
I"m not an ASP guy but...

Try putting a breakpoint at line 7. You can then hove the mouse over each part of
level1Authorizations.Items.
In theory you should see level1Authorizations in the "locals" tab, but if you don't have that open you can hover over "Items" and the Intellisense will let you view the items list.

Its a start
Jun 3 '09 #2
Frinavale
9,735 Expert Mod 8TB
The Item count is supposed to be 0 if you're using a DataSource.

If you want to use the CheckBox.Items.Count property to retrieve the count then you will have to loop through the data source you're using and add each thing found as a ListItem to the CheckBoxList's Items property:
Expand|Select|Wrap|Line Numbers
  1. For each thing in myDataSource
  2.   Dim newItem As New ListItem
  3.   newItem.Text = thing.Text
  4.   myCheckBoxList.Items.Add(newItem)
  5. Next
  6.  
However if you want to continue using the DataSource Object then you should be asking the DataSource Object how many items it has.

-Frinny
Jun 3 '09 #3
tlhintoq
3,525 Expert 2GB
@Frinavale
I'm learning too....
So what you're saying is that the ListBox never actually *has* the items it's displaying? Although they show in the ListBox, and act like they are a part of it's items collection in that they can be selected... because they are bound data its really an illusion? The items displayed by the binding are more like pointers or shortcuts to the real items?
Jun 3 '09 #4
Now that I know the count is supposed to be 0. How do I get into the checkboxlist so I can check the appropriate boxes on load. I can not use a foreach loop it just ignores it/
Expand|Select|Wrap|Line Numbers
  1.  foreach (ListItem checkBox in level1.Items) 
  2. {
  3.   checkBox.Selected=false;
  4. }
  5.  
Jun 3 '09 #5
Frinavale
9,735 Expert Mod 8TB
I haven't had much experience with this....especially with regards to ASP.NET

I gave it a quick test to make sure that I was making sense and it appears that my first response is incorrect. (I came to this conclusion once before when doing something similar...except it was a desktop application....I don't know why I thought this)

The items actually do show up but only after you've done your DataBind() on the ListBox (or CheckBoxList).

Quick example:

Here's the code I have for some Business Object that I created:
Expand|Select|Wrap|Line Numbers
  1. Public Class BusObj
  2.     Private _sourceOfData As DataSet
  3.  
  4.     Public Sub New()
  5.         Dim someTable As New DataTable
  6.         someTable.Columns.Add("FirstName")
  7.         someTable.Columns.Add("LastName")
  8.         someTable.Columns.Add("HomePhone")
  9.  
  10.         For i As Integer = 0 To 5
  11.             Dim dr As DataRow
  12.             dr = someTable.NewRow
  13.             dr("FirstName") = "FirstName" + i.ToString
  14.             dr("LastName") = "LastName" + i.ToString
  15.             dr("HomePhone") = i.ToString + i.ToString + i.ToString + "-" + i.ToString + i.ToString + i.ToString + i.ToString
  16.             someTable.Rows.Add(dr)
  17.         Next
  18.         _sourceOfData = New DataSet
  19.         _sourceOfData.Tables.Add(someTable)
  20.     End Sub
  21.     Public Function GetPeople() As DataSet
  22.         Return _sourceOfData
  23.     End Function
  24. End Class
This business object can be used with the ObjectDataSource control so that the CheckBoxList can be bound to it.

Here is the ASP markup:
Expand|Select|Wrap|Line Numbers
  1. <asp:CheckBoxList ID="objDataSourceTest" runat="server" DataTextField="FirstName" DataValueField="LastName" >
  2. </asp:CheckBoxList>
  3.  
  4. <asp:ObjectDataSource ID="theSource" runat="server" SelectMethod="GetPeople" TypeName="MyNamespace.BusObj" >
  5. </asp:ObjectDataSource>
  6.  
Note that I have declaratively set the DataValueField and DataTextField properties for the CheckBoxList . These indicate which files to bind onto...

So this CheckBoxList will display all of the First Names as text and the LastNames are not displayed (they are the values...this would make more sense if you were using RadioButtonList instead of a ChecKBoxList because the CheckBoxList doesn't make use of the values). If you wanted to change this so that it displayed the "FirstName LastName" as text you'd add a property to the business object that exposes the FullName and set that as the DataTextField.

In the VB code (code behind) I set the CheckBoxList's DataSource to the "theSource" ObjectDataSource and call the DataBind method (in the page load). For some reason you cannot declaratively set the DataSource for a CheckBoxList (I got an exception indicating that this wasn't supported when I tried...I'm probably using the wrong property but don't feel like looking it up right now).
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.         objDataSourceTest.DataSource = theSource
  3.         objDataSourceTest.DataBind()
  4. End Sub
After I call the DataBind method the Items are available....so now I can loop through the items and select them:
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.         objDataSourceTest.DataSource = theSource
  3.         objDataSourceTest.DataBind()
  4.  
  5.        For Each thing As ListItem In objDataSourceTest.Items
  6.             thing.Selected = True
  7.        Next
  8. End Sub
Jun 3 '09 #6
I found what I needed. I adapted some code from asp.net forum. This allows me to compare the items in my checkboxlist with the list I get from my table and check the boxes accordingly. yeah!
Expand|Select|Wrap|Line Numbers
  1.   protected void Auth_DataBound(object sender, EventArgs e)
  2.     {
  3.             DataManager Data = new DataManager();
  4.             Int32 ID = Convert.ToInt32(Request.QueryString["id"].ToString());
  5.             DataTable authDataTable = Data.GetSelectedAuth(ID);
  6.             List<string> selectAuth = new List<string>();
  7.             foreach (DataRow row in authDataTable.Rows)
  8.             {
  9.                 selectAuth.Add(row["AuthID"].ToString());
  10.             }
  11.  
  12.             ListControl listbox = (ListControl)sender;
  13.             BindMultiSelectList(listbox, selectAuth);
  14.         }
  15.  
  16.  
  17.     public  void BindMultiSelectList(ListControl listBox, List<string> selectedIds)
  18.     {
  19.         foreach (ListItem item in listBox.Items)            
  20.         { 
  21.             foreach (string id in selectedIds) 
  22.             { if (item.Value == id)                 
  23.                 item.Selected = true; 
  24.             } 
  25.         } 
  26.     }  
Jun 3 '09 #7
Frinavale
9,735 Expert Mod 8TB
Yup this works.

You're implementing a method that handles the DataBound event for the CheckBoxList.....at which point you're setting the items to selected. It's a lot more complicated than my solution but it works :)

Glad you solved your problem!
Jun 3 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bryce Budd | last post by:
Hello All, I've been a taker of information from newsgroups for a long time and thought I'd finally make a contribution back to the community whose supported me when I've needed it. After all...
4
by: Shaul Feldman | last post by:
Hello, I have something really awkward at work - fighting with CheckBoxList... How can I define CSS for ListItem in CheckBoxList programmatically. I add CheckBoxList's Items on the fly, something...
2
by: Ratman | last post by:
I have the following function that created a checkboxlist and "is supposed" to be checking values that are already saved in the database. As I step thru the code, it is in fact marking the...
5
by: Eirik Eldorsen | last post by:
I'm trying to code a reapter that for each listelement show a checkboxlist. I'm almost there. The only thing I can't figure out is how to set the ID of the checkboxlists. This is my code:...
3
by: I am Sam | last post by:
I keep getting the following error message when I try to iterate through a CheckBoxList control: Object reference not set to an instance of an object. Description: An unhandled exception...
5
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able...
4
by: Patrick.O.Ige | last post by:
I have a CheckBoxList in a DataList and i'm trying to get item Selected after doing a postBack. I have set my CheckBoxlist AutoPostBack="True" Any ideas what 'm doing wrong? It seems not to...
2
by: Patrick.O.Ige | last post by:
I have some boolean value(1 or 0 ) in a table and i want a databinded CheckBoxList to present the selected values on the page.. With CheckBox i know i can se the Checked property like so :-...
0
by: Jai | last post by:
Hi, Somebody please tell me how to bind(two way) a checkboxlist with objectdatasource if the checkboxlist is inside a formview..... Code of FormView is like this::--- <asp:FormView...
0
by: Jai | last post by:
Hi, Somebody please tell me how to bind(two way) a checkboxlist with objectdatasource if the checkboxlist is inside a formview..... Code of FormView is like this::--- <asp:FormView...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.