473,769 Members | 7,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically created webcontrol's events not firing

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
SelectedIndexCh anged event. I'm adding the dynamic control like this:

mtblCell = new TableCell();
mtblCell.Column Span = 2;
DropDownList cmbShipping = new DropDownList();
cmbShipping.Ite ms.Add(new ListItem("", "0"));
cmbShipping.Ite ms.Add(new ListItem("Unite d Kingdom", "1"));
cmbShipping.Ite ms.Add(new ListItem("Europ ean Union", "2"));
cmbShipping.Ite ms.Add(new ListItem("Rest of world", "3"));
cmbShipping.Aut oPostBack = true;
cmbShipping.Sel ectedIndexChang ed += new
EventHandler(cm bShipping_Selec tedIndexChanged );
mtblCell.Contro ls.Add(cmbShipp ing);
mtblRow.Cells.A dd(mtblCell);
I've already created the code for the event:

private void cmbShipping_Sel ectedIndexChang ed(object sender, EventArgs e)
{
BindData();
}
It all seems to work well enough inasmuch as the DropDownList certainly does
get created in the TableCell - the resulting HTML looks like this:

<td colspan="2">
<select name="_ctl0" onchange="__doP ostBack('_ctl0' ,'')"
language="javas cript">
<option value="0"></option>
<option value="1">Unite d Kingdom</option>
<option value="2">Europ ean Union</option>
<option value="3">Rest of world</option>
</select>
</td>
When I change the selected index of the DropDownList, it does the postback
as expected. However, the cmbShipping_Sel ectedIndexChang ed event doesn't
fire. I must be missing something totally obvious.

Any assistance gratefully received.

Mark
Nov 19 '05 #1
3 3346
Hi,

For these events to get wired up properly, for a DropDownList, you need
to add the controls in the OnInit() method of the page. If you add
these controls only for certain rows, I would add them but make them
invisible until you decide they need to be shown.

Thanks,

Sayed Y. Hashimi

http://www.sayedhashimi.com
Shameless Book Plug: Service-Oriented Smart Clients with .NET 2.0
http://www.amazon.com/exec/obidos/tg...glance&s=books

Nov 19 '05 #2
<ha**********@g mail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
For these events to get wired up properly, for a DropDownList, you need
to add the controls in the OnInit() method of the page. If you add
these controls only for certain rows, I would add them but make them
invisible until you decide they need to be shown.


Sorry, I failed to mention that I'd already tried that... No matter, I found
another way to do it - thanks for the reply.
Nov 19 '05 #3
jim
What was the solution you found? Please share

"Mark Rae" wrote:
<ha**********@g mail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
For these events to get wired up properly, for a DropDownList, you need
to add the controls in the OnInit() method of the page. If you add
these controls only for certain rows, I would add them but make them
invisible until you decide they need to be shown.


Sorry, I failed to mention that I'd already tried that... No matter, I found
another way to do it - thanks for the reply.

Nov 19 '05 #4

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

Similar topics

5
2146
by: MS Newsgroups | last post by:
Hi, I have a scenario where I am dynamically adding a control from code when a controls event is fired. The problem I have is that when the newly created control is clicked, the click event does not fire. I have written a simple demonstration that reproduces the problem and would appreciate if someone could give me a hint on how this should be done Many thanks Niclas
1
8167
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...
2
2232
by: JezB | last post by:
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm having trouble attaching events to these. For example, adding an image button :- ImageButton nb = new ImageButton(); nb.ImageUrl = "text.gif"; nb.ToolTip = "Edit Text"; nb.Click += new ImageClickEventHandler(b1_Click); myPlaceholder.Controls.Add(nb);
1
1638
by: Bob | last post by:
I have a requirement to show a label and x number of buttons in all of the pages. So i decided to write a Base Page class and added code for dynamically adding the label and buttons. For the buttons i have added the handlers. Finally added the label and buttons to a place holder. When page is rendered i am able to see the label and buttons. but when i click the buttons events are not firing. i found two problems. First one is when i...
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...
5
4396
by: bryanp10 | last post by:
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 CreateChildControls() for my custom control. Here's a snippet of code: DropDownList dd = new DropDownList(); dd.ID = "DropDown1"; if( !Page.IsPostBack )
1
1499
by: AndrewMBaldwin | last post by:
Ok, this is going to be a long post, so I apologize in advance, but if it was an easy question I would have probably found an answer somewhere out here by now... The short story of this is that I am creating a "process framework" that allows for common web controls to be linked together in a procedural fashion, and will also allow for code to be added to these controls (IE New button events, etc). I have basically created a static...
4
3278
by: Keith Patrick | last post by:
I'm running into a wall with a custom control that gets populated via asynchronous I/O. Basically, my control takes its datasource and renders some controls that have events (OnClick). However, there's the well-known problem of dynamic controls not firing unless they are created during the page's initialization, so OnPreRender is too late to create any dynamic controls. That being the case, how can I populate a control asynchronously...
2
10077
by: jasinx | last post by:
When I run this code it creates a button on an .aspx page. When I click the button it seems to fire the events but I'm unable to capture them in the function in the ascx codebehind. Any help? Thank Jason. start ASCX codebehind ------------------------------------------- public partial class WebUserControl1 : System.Web.UI.UserControl { Button btn = new Button();
0
9423
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
10219
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...
0
10049
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
9998
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
8876
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...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.