all groups > asp.net webcontrols > december 2005 >
You're in the

asp.net webcontrols

group:

CheckBoxList - When does it actually databind?


CheckBoxList - When does it actually databind? beaudetious
12/29/2005 7:48:03 PM
asp.net webcontrols:
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,
RE: CheckBoxList - When does it actually databind? Phillip Williams
12/29/2005 8:40:05 PM
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);
}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
RE: CheckBoxList - When does it actually databind? beaudetious
12/30/2005 3:14:01 PM
I ended up just calling the CheckBoxList's DataBind() routine inside of
Page_Load and I get what I need. Thanks.

[quoted text, click to view]
AddThis Social Bookmark Button