DropDownList refusing to data bind in dynamically created ascx, help!

G

Guest

I've run into a problem where drop down lists are refusing to data bind with the following error on the DataBind() call

Specified argument was out of the range of valid values. Parameter name: value

Stack trace is showing the error in OnDataBinding event, which I don't hook in the cases where I'm getting this error
ArgumentOutOfRangeException: Specified argument was out of the range of valid values
Parameter name: value
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e
System.Web.UI.Control.DataBind(
Intercede.WebSite.CompanyLocationFieldControl.OnPreRender(EventArgs e) in c:\documents\projects\intercede\intercederelease1\website\reporting\companylocationfield.ascx.cs:9
System.Web.UI.Control.PreRenderRecursiveInternal(
System.Web.UI.Control.PreRenderRecursiveInternal(
System.Web.UI.Control.PreRenderRecursiveInternal(
System.Web.UI.Control.PreRenderRecursiveInternal(
System.Web.UI.Control.PreRenderRecursiveInternal(
System.Web.UI.Control.PreRenderRecursiveInternal(
System.Web.UI.Control.PreRenderRecursiveInternal(
System.Web.UI.Control.PreRenderRecursiveInternal(
System.Web.UI.Page.ProcessRequestMain(

In each case, the DropDownList datasource is set to a class derived from CollectionBase that contains a list of custom classes. This code used to works in a declared ascx, but I've since moved the code to ascx controls that are dynamically created with Page.LoadControl(...). How this matters I haven't determined, but it seems to. If I move the code back to an ascx that is declared in the hosting aspx, the problem goes away

Any ideas? Does dynamically creating a control with Page.LoadControl() affect data binding events somehow? I'll try to post a code snippet if I can narrow down a reasonable chunk
 
G

Guest

Try using ParseControl instead of LoadControl to load custom user controls. Also I noticed any attempts to register the control on the page using <% %> is ignored by the ParseControl method but you can still load you controls by placing the <% %> control register directive inside the string parameter passed to ParseControl..

something like this, i forgot the exact syntax.

this.Controls.Add(this.ParseControl("<% Control register directive.... blah blah tagName=\"myControls\" prefix=\"general\"%><general:myControls id=someId runat=server></general:myControls

Also, when dynamically adding controls be sure to add them before ViewState is loaded if you want them to work properly. If you add them after ViewState is loaded it will be like you added a new control with an empty ViewState... dynamically added controls are a bit of pain in the ... neck...
 
G

Guest

I figured out the problem; based on a complex series of events with the dynamic controls, the DropDownList.SelectedValue property was getting set before the data was loaded. This caused the DataBind to fail. Why the DropDownList decides to do this, I don't know, it sure would have been easier to find if it just told me it couldn't select that item when I tried to set it :

Now to try and fix my ViewState problems with the dynamic controls...
 
V

vpdsouza

John,

Could you tell me what workaround you did for your problem. I have th
same problem. I am using DataTable to populate the DropDownList (DDL
and it works fine to load at the first time. But if I change the ta
then click on the menu to startover I get this error. The same cod
works in normal mode but on the click of the Menu item. Everything i
same because same Sub has been called from everywhere! The worst par
is it fails on the DataBind call which is bound to the DataTable.

Any help is greatly appreciate
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top