|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
CheckBoxList - When does it actually databind?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 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 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
programmatically copy table to another application
What functionality does Infragistics controls offer, which don't come with VS .NET 2005? grid postback Referencing controls inside Wizard control newbie to creating web controls Closing window/page Datagrid Basic formatting for HTML text in ASP.NET 1.1 ? Listbox is overlapping the Textbox in Asp.net Adding a Directive to a dynamically created Template Column on gri |
|||||||||||||||||||||||