CheckBoxList - When does it actually databind?

B

beaudetious

I've got an ASP.NET 2.0 form with a checkboxlist control bound to a
SqlDataSource control. In the page's Page_Load event I want to loop through
the items in this CheckBoxList and programmatically select a few items.
However, when I do this in a foreach block I find that the ItemsCollection
contains 0 items. So, when is the best place to perform this task if not the
Page_Load event (checking for !IsPostBack of course).

Thanks,
beaudetious
 
P

Phillip Williams

The event DataBound marks the completion of the control databinding:

//At this stage the control is not databound yet. The list count is 0
void Page_Load(Object sender, EventArgs e)
{
Response.Write("<br>Page_Load: CheckBoxList1.Items.Count= " +
CheckBoxList1.Items.Count);
}
//the databound event is raised when the control is databound
void CheckBoxList1_DataBound(object sender, EventArgs e)
{
//this should print the total number of ListItems
Response.Write("<br>CheckBoxList1_DataBound:
CheckBoxList1.Items.Count= " + CheckBoxList1.Items.Count);
foreach (ListItem li in CheckBoxList1.Items)
{
//process each listitem
}
}
//At this stage the control is populated.
void Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("<br>Page_PreRenderComplete:
CheckBoxList1.Items.Count= " + CheckBoxList1.Items.Count);
}
 
B

beaudetious

I ended up just calling the CheckBoxList's DataBind() routine inside of
Page_Load and I get what I need. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top