473,670 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
ArgumentOutOfRa ngeException: Specified argument was out of the range of valid values
Parameter name: value
System.Web.UI.W ebControls.List Control.OnDataB inding(EventArg s e
System.Web.UI.C ontrol.DataBind (
Intercede.WebSi te.CompanyLocat ionFieldControl .OnPreRender(Ev entArgs e) in c:\documents\pr ojects\interced e\intercederele ase1\website\re porting\company locationfield.a scx.cs:9
System.Web.UI.C ontrol.PreRende rRecursiveInter nal(
System.Web.UI.C ontrol.PreRende rRecursiveInter nal(
System.Web.UI.C ontrol.PreRende rRecursiveInter nal(
System.Web.UI.C ontrol.PreRende rRecursiveInter nal(
System.Web.UI.C ontrol.PreRende rRecursiveInter nal(
System.Web.UI.C ontrol.PreRende rRecursiveInter nal(
System.Web.UI.C ontrol.PreRende rRecursiveInter nal(
System.Web.UI.C ontrol.PreRende rRecursiveInter nal(
System.Web.UI.P age.ProcessRequ estMain(

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.LoadContro l(...). 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.LoadContro l() 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 2867
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.A dd(this.ParseCo ntrol("<% Control register directive.... blah blah tagName=\"myCon trols\" prefix=\"genera l\"%><general:m yControls id=someId runat=server></general:myContr ols

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.Se lectedValue 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
17426
by: Fabio Negri Cicotti [MCP] | last post by:
Sorry for my silly question... in the same way we can define in a DropDownList the "ValueMember" and "Display Member" when we bind a DatasSource, how could I do it using a hard-code fixed datasource (without neither access the DB or create a DataTable)? To be as clear as possible, if I want to fill a DropDownList with the following data (not comming from database, but from my source-code), how do I do?
6
7004
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone changes box1, I want to set the selected index in box2 = 0. When I do this, I dont get an error, but when the page loads, it still has the selected value and not 0. It seems that it is getting the selected value from the viewstate
18
2440
by: Julia Hu | last post by:
Hi, I have a datagrid, and in different rows I need to programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a textbox, and in the second row I need to bind data into a dropdownlist...It all depends on the data I select from the database. I cannot use TemplateColumn because it has to be the same type of control for one column.
2
3045
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.. only the labels (caption) and the invisible literal controls are binded.. Now based on the invisible literal control values I get the information from DB to bind it with each dropdown list (I am doing this in the item databound event) Also based on...
8
3382
by: utterberg | last post by:
Hi I'm adding two dropdownlist in my webform by clicking a button. I fill the first dropdown with some values from my DB and the other one when I choose from the first dropdown. But since theese dropdownlists are created by clicking a button they no longer exists on my webform. How do I keep my dynamically created dropdown when using OnSelectedIndexChanged? Can anyone help me solve this problem? Or is there a better way of doing this?
0
1183
by: ASP Developer | last post by:
I created a very simple user control that has one dropdownlist. I bind the dropdownlist to database results on load in an ascx file. I would like to do be able to change the selectedindex from the aspx page. So I created a property in the ascx file to expose the selected index property. However, when I attempt to change the selectedindex I get the error: object is not set to an instance of an object". Any help would be greatly...
2
2016
by: Nosaj | last post by:
Houston, we've got a problem... In a ascx file, I bind a datasource with a dropdownlist. The following code runs perfectly in a aspx file but not in a ascx file. And I just can't understand it. I changed many time my Page_Load function, but nothing happened... Function MyQueryMethod() As System.Data.DataSet Dim connectionString As String = "server='XXX'; user id='XXX'; password='XXX'; database='XXX'"
0
1629
by: Jose Fernandez | last post by:
Hello First of all, excuse my english We all know that in a normal page with codebehind we add a dropdownlist control and we bind() it in the (!ISPOSTBACK), so, we don't need to go to database again 'cause its value are saved (i assume viewstate). Well. I have a class that inherits from WebControl and INamingContainer. This class Load an ASCX control (that contains other controls in itself like textbox and Dropdownlist). In the...
1
4923
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over time. The problem is that when an item has been removed from the lookup table, and a user wants to retrieve a record that used the deleted item, the following error occurs: 'ddlAssignedTo' has a SelectedValue which is invalid because it...
0
8469
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2800
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.