473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

mtblCell = new TableCell();
mtblCell.ColumnSpan = 2;
DropDownList cmbShipping = new DropDownList();
cmbShipping.Items.Add(new ListItem("", "0"));
cmbShipping.Items.Add(new ListItem("United Kingdom", "1"));
cmbShipping.Items.Add(new ListItem("European Union", "2"));
cmbShipping.Items.Add(new ListItem("Rest of world", "3"));
cmbShipping.AutoPostBack = true;
cmbShipping.SelectedIndexChanged += new
EventHandler(cmbShipping_SelectedIndexChanged);
mtblCell.Controls.Add(cmbShipping);
mtblRow.Cells.Add(mtblCell);
I've already created the code for the event:

private void cmbShipping_SelectedIndexChanged(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="__doPostBack('_ctl0','')"
language="javascript">
<option value="0"></option>
<option value="1">United Kingdom</option>
<option value="2">European 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_SelectedIndexChanged event doesn't
fire. I must be missing something totally obvious.

Any assistance gratefully received.

Mark
Nov 19 '05 #1
3 3328
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**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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
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...
1
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...
2
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();...
1
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...
3
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,...
5
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...
1
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...
4
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,...
2
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.