469,924 Members | 1,418 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,924 developers. It's quick & easy.

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


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.OnDataBindin g(EventArgs e
System.Web.UI.Control.DataBind(
Intercede.WebSite.CompanyLocationFieldControl.OnPr eRender(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

Nov 18 '05 #1
3 2593
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...
Nov 18 '05 #2
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...
Nov 18 '05 #3

John,

Could you tell me what workaround you did for your problem. I have the
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 tab
then click on the menu to startover I get this error. The same code
works in normal mode but on the click of the Menu item. Everything is
same because same Sub has been called from everywhere! The worst part
is it fails on the DataBind call which is bound to the DataTable.

Any help is greatly appreciated
--
vpdsouza
------------------------------------------------------------------------
vpdsouza's Profile: http://www.highdots.com/forums/m1827
View this thread: http://www.highdots.com/forums/t643200

Feb 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Fabio Negri Cicotti [MCP] | last post: by
6 posts views Thread by Robin Bonin | last post: by
2 posts views Thread by Shiju Poyilil | last post: by
reply views Thread by Jose Fernandez | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.