473,758 Members | 4,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically created DropDownList not firing events or persisting viewstate

I have a page that is almost entirely dynamically created. Textboxes
and checkbox are working fine, firing events, and persistent their
state. DropDownList is giving me a major headache.

All my controls are created in CreateChildCont rols() for my custom
control. Here's a snippet of code:

DropDownList dd = new DropDownList();
dd.ID = "DropDown1" ;
if( !Page.IsPostBac k )
{
//dd.DataSource = GetListItems();
//dd.DataBind();
dd.Items.Add( new ListItem("Apple s", "1") );
dd.Items.Add( new ListItem("Banan as", "2") );
dd.Items.Add( new ListItem("Orang es", "3") );
int i = dd.Items.IndexO f( new ListItem( user.FruitChoic e ));
if( i != -1 ) dd.SelectedInde x = i;
}
dd.SelectedInde xChanged +=new EventHandler(dd _SelectedIndexC hanged);
//dd.AutoPostBack = true;
Controls.Add(dd );

The event handler never executes. My list items are not present after
a post-back. How do I persist this and get the data out of it? Why is
my event handler not firing?

I've tried re-creating the list items every time... I've tried the
autopostback obviously... I've tried explicitly assigning a control ID.
Nothing is working.

I've read a *ton* of old newsgroup posts about this very subject over
the last hour or two, but either it's a different problem or no
solution posted.

Anybody have some pointers here?

-Bryan

Nov 19 '05 #1
5 4395
Just out of curiosity, I note that you have the AutoPostBack line
commented out. Did you try to set the AutoPostBack to true manually
and this still happens? By default, DDLs are AutoPostBack=fa lse.
Have you tried to debug this to see where the events are actually
going?
Just a thought,
Geek Rex

On 10 Mar 2005 15:18:27 -0800, "br******@hotma il.com"
<br******@hotma il.com> wrote:
I have a page that is almost entirely dynamically created. Textboxes
and checkbox are working fine, firing events, and persistent their
state. DropDownList is giving me a major headache.

All my controls are created in CreateChildCont rols() for my custom
control. Here's a snippet of code:

DropDownList dd = new DropDownList();
dd.ID = "DropDown1" ;
if( !Page.IsPostBac k )
{
//dd.DataSource = GetListItems();
//dd.DataBind();
dd.Items.Add( new ListItem("Apple s", "1") );
dd.Items.Add( new ListItem("Banan as", "2") );
dd.Items.Add( new ListItem("Orang es", "3") );
int i = dd.Items.IndexO f( new ListItem( user.FruitChoic e ));
if( i != -1 ) dd.SelectedInde x = i;
}
dd.SelectedInde xChanged +=new EventHandler(dd _SelectedIndexC hanged);
//dd.AutoPostBack = true;
Controls.Add(dd );

The event handler never executes. My list items are not present after
a post-back. How do I persist this and get the data out of it? Why is
my event handler not firing?

I've tried re-creating the list items every time... I've tried the
autopostback obviously... I've tried explicitly assigning a control ID.
Nothing is working.

I've read a *ton* of old newsgroup posts about this very subject over
the last hour or two, but either it's a different problem or no
solution posted.

Anybody have some pointers here?

-Bryan


Nov 19 '05 #2
Yes, I have tried with AutoPastBack.
Have you tried to debug this to see where the events are actually

going?

I'm not sure what you mean by this?

Actually, it's just the SelectedIndexCh anged event that is not firing.
The Load and Unload events are firing just fine. Which makes it all
the more frustrating...

Nov 19 '05 #3
Where are you building the list? Dynamically built lists need to be built
during the Init event, otherwise event will not fire.

<br******@hotma il.com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com...
Yes, I have tried with AutoPastBack.
Have you tried to debug this to see where the events are actually

going?

I'm not sure what you mean by this?

Actually, it's just the SelectedIndexCh anged event that is not firing.
The Load and Unload events are firing just fine. Which makes it all
the more frustrating...

Nov 19 '05 #4
I am creating the DropDownList in CreateChildCont rols. That certainly
seemed like the logical place to do it, given the method name. It's
where I'm creating everything else and those controls are working just
fine.

Nov 19 '05 #5
OK, I just tried building in the OnInit event and sure enough the
SelectedIndexCh anged event fired correctly this time. Thanks for the
info Peter.

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5482
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
8
4315
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image button at runtime: //----- Code snippet protected System.Web.UI.WebControls.PlaceHolder ImageHolder; private void Page_Load(object sender, System.EventArgs e)
1
8166
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the first time the control is dynamically loaded, everything works fine. After that, if the control is loaded again from the page button event handler, the user controls events fail to fire on the first click NOTE: I (believe I) am rebuilding all...
3
4323
by: JoakimR | last post by:
Hello, I have a problem with events not firing. I've created a web user control which renders a calendar using a table control. In two cells I'm adding LinkButtons for "prev/next month". However, the events from these are not fired. In the PageLoad event I check if it's a PostBack. If it isn't, I'm calling DisplayCalendar() to render the calendar. This part works and I get the output. If it *is* a postback (i.e. one of the linkbuttons...
3
3346
by: Mark Rae | last post by:
Hi, I'm sure I've done this before, but I can't get it to work today... I'm trying to add a DropDownList into a TableCell and wire up its SelectedIndexChanged event. I'm adding the dynamic control like this: mtblCell = new TableCell(); mtblCell.ColumnSpan = 2; DropDownList cmbShipping = new DropDownList();
1
1453
by: Chad Smith | last post by:
Hi. I have an asp.net web application written in C# which creates a datasource consisting of a Name and a list of permissable Values. This datasource is then bound to a simple repeater, the Name being bound to a label and the Values being bound to a DropDownList. All works fine. The next thing I wished to do was tap into the ItemDataBound event to add a generic "Please Select..." item to each DropDownList, with a value of "0". From...
3
2542
by: Dotnet Gruven | last post by:
I've built a WebForm with a Table added dynamically in Page_Load when IsPostBack is false. The table includes a couple of TextBoxes, RadioButtonLists and CheckboxLists. On postback, those controls are not present nor their valves in Request.Form.AllKeys.
5
2724
by: Steve Moreno | last post by:
Hi all, I've got a web form that I've written code to create an array of DropDownList controls on the page depending on how many records are pulled back. The code to create the controls is working fine but now I need to add events to the newly created DropDownList controls. I need to add the SelectedIndexChanged event and I'm having a hard time getting the code to add events to the controls since the names (IDs) are varied to include...
5
6760
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created link buttons to stay wired up to their click events. I have what is basically a simply survey question generation page. The page first displays a few static fields and a dropdownlist of various options for the user to select. When the user...
0
9492
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
9908
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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
9740
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8744
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
5175
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
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3832
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
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.