Dynamic DropDownList in CompositeControl

woulsessy

New Member
I need first to apologise for my potential ugly english :)I am currently working on a composite control, in this control, I would like to use a DropDownList with a dynamic datasource. For instance:
  • The user choose the "itemPerPage" value through a TextBox.
  • The TextBox fires the "TextChanged" event
  • Then I rebuild the child control hierarchy through EnsureChildControls method.
In the CreateChildControl method: I get the number of pages returned by the database, and then I recreate the DropDownList with a dynamic generated datasource (the page indices).The problem:Now the user selects a page index through the generated DropDownList, the SelectedIndexChanged event is thrown, but it seems, on postback, that the DropDownList state is not restored from viewstate: ((DropDownList)sender).SelectedValue still the same: 0.In short: is it possible, in a composite control, to use a dropDownList with a dynamic datasource ? if yes, how to do it properly ?if I used a static source, I would have assigned it once before any postback, but i really need a dynamic datasource for this dropDownList.I hope you anderstand my question, i can post some code if needed.Ask me if you need more details.Best regards,Quick[EDIT]some code to illustrate (In CreateChildControls):\[code\]var tmpDatasource = filter == null ? datasource : datasource.Where(filter); var itemCount = tmpDatasource.Count(); int pageCount = itemCount / itemPerPage; if (itemCount % itemPerPage != 0) pageCount++; ddlPageIndex.AutoPostBack = true; ddlPageIndex.EnableViewState = true; ddlPageIndex.SelectedIndexChanged += new EventHandler(ddlPageIndex_SelectedIndexChanged); ddlPageIndex.DataValueField = "Value"; ddlPageIndex.DataTextField = "Label"; //Should be wrapped by if (!Page.IsPostBack) {... ddlPageIndex.DataSource = Enumerable.Range(1, pageCount).Select(x => new { Label = x.ToString(), Value = http://stackoverflow.com/questions/13766240/(x - 1).ToString() }).ToArray(); //} pageIndexCell.Controls.Add(ddlPageIndex);\[/code\]
 
Back
Top