Home All Groups Group Topic Archive Search About

CheckBoxList - When does it actually databind?

Author
30 Dec 2005 3:48 AM
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

Author
30 Dec 2005 4:40 AM
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);
    }
Show quoteHide quote
"beaudetious" wrote:

> 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
Author
30 Dec 2005 11:14 PM
beaudetious
I ended up just calling the CheckBoxList's DataBind() routine inside of
Page_Load and I get what I need.  Thanks.

Show quoteHide quote
"Phillip Williams" wrote:

> 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
>
>
> "beaudetious" wrote:
>
> > 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