473,241 Members | 1,600 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,241 software developers and data experts.

Dynamic Events from Dynamic Controls on a User Control - Guess What? Broken!


Hope you can help.

I am relatively new to all this but would appreciate the groups help.

The scenario:

I am c# and asp.net to create a web application. The web page contains
a user control (.ascx).
This user control DYNAMICALLY loads the controls it is to show from an
XML file.

(The idea is that the same user control - a "search" control - in many
different circumstances - the programmer just providing XML with the
parameters to be searched on.

Anyway, the controls load fine. The code for adding the events also
compiles and runs fine - but, yes you guessed it - the event is never
triggered. Here's some code - ignore the quality of the code - what I'm
after here is a fix firstly...


string sXSLT =
System.Configuration.ConfigurationSettings.AppSett ings["XSLT"];
string sControls =
System.Configuration.ConfigurationSettings.AppSett ings[XMLFile];

// Load the data source

XPathDocument UserSearchControl = new

// Load the xslt to do the transformations
XslTransform transform = new XslTransform();


// Get the transformed result
StringWriter sw = new StringWriter();
transform.Transform(UserSearchControl, null, sw);
string result = sw.ToString();

// remove the namespace attribute
result = result.Replace("xmlns:asp=\"remove\"", "");

// parse the control(s) and add it to the page
Control ctrl = Page.ParseControl(result);
AddEvents (ctrl);
And also:

private void AddEvents(Control Parent)
// go through each drop down list... if any hidden controls are
// then show or hide appropriately....
foreach (Control c in Parent.Controls)
(c.GetType().ToString().Equals("System.Web.UI.WebC ontrols.DropDownList"))
DropDownList ddl = (DropDownList)c;
//if (ddl.AutoPostBack)
// ddl.SelectedIndexChanged += new System.EventHandler(SaveData);
ddl.SelectedIndexChanged +=new
if (c.Controls.Count > 0)

As you will see, if the dynamic control is a drop down list then Im
adding the event "SaveData".

It doesn't matter what's in "SaveData" as it never gets there -

Note (1) the controls are added in OnInit.
Note (2) the event for each ddl is added before the controls are added
to the page. - have tried doing it afterwards also!

If I add the following rough code (!) before all of the above:
DropDownList ddl = new DropDownList();
ddl.ID = "kfkfk";
ListItem itm = new ListItem();
itm.Text = "kkk";
ListItem itmd = new ListItem();
itmd.Text = "545";
ddl.SelectedIndexChanged +=new EventHandler(SaveData);

The above SelectedIndexChanged event will trigger my event "SaveData".

So, I'm stumped.

My current thinking is to scrap the the idea of adding all of the
dynamic controls in one pass. Rather, do it one at a time, adding the
event at the same time. But that's messy - and probably wont work.


Thanks in advance!

Nov 19 '05 #1
5 2375
Where's your AddEvent method called?

Nov 19 '05 #2
Oops, nm, seen it.

Not sure it can help, but there's an article in msdn there :
that could help you

Nov 19 '05 #3
It's within USERCONTROL OnINIT - second to last line
So, I create the controls (works), add events to those controls (works)
and then add the controls to the page - and all within OnInit. But the
event does not fire!

Nov 19 '05 #4
Thanks - but that's not quite what I'm after.

The issue is that a control dynamically added via my XML never fires an
event, but if I type it in to dynamically add it the event fires.

I'm going bald!

Nov 19 '05 #5
Thanks - but that's not quite what I'm after.

The issue is that a control dynamically added via my XML never fires an
event, but if I type it in to dynamically add it the event fires.

I'm going bald!

Nov 19 '05 #6

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

Similar topics

by: MikeY | last post by:
Hi Everyone, I am working in C#, windows forms.My question is this. All my button dynamic controls properties are present and accounted for except for the"FlatStyle" properties. I can't seem to...
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
by: Michael Ramey | last post by:
Howdy, I think I have a good question! I'm creating a usercontrol, and within this usercontrol, I'm dynamically creating controls (imagebuttons to be exact), that the user can click on which...
by: Chris Thunell | last post by:
I have created several grids dynamically and have added them to different HTML placeholders on a vb.net web form. The grids and controls within them come up and view beautifully when the web page...
by: Brian Shannon | last post by:
I have 3 combo boxes and two date text boxes on a .aspx page. The user can fill in any of the 5 controls or none to filter a datagrid. I was hoping someone could explain how to efficiently build...
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle...
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
by: msdev | last post by:
Hello, I am creating my own webbrowser to learn VB .Net. I am stuck on an issue with regards to dynamically-created controls, in this case tabs on a tabcontrol and webbrowsers created within...
by: HP | last post by:
Hi there The problem of dynamically created controls vs viewstate is widely known one. To access values of controls they have to be recreated on Page_Load. Unfortunately it causes many...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.