470,638 Members | 1,633 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,638 developers. It's quick & easy.

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

Hi,

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...

So...
Within USERCONTROL OnINIT

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

// Load the data source

XPathDocument UserSearchControl = new
XPathDocument(Server.MapPath(sControls));

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

transform.Load(Server.MapPath(sXSLT));

// 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);
this.Controls.Add(ctrl);
---------------
And also:

private void AddEvents(Control Parent)
{
// go through each drop down list... if any hidden controls are
attached
// then show or hide appropriately....
foreach (Control c in Parent.Controls)
{
c.EnableViewState=true;
if
(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
EventHandler(ddl_SelectedIndexChanged);
}
if (c.Controls.Count > 0)
ShowHideHiddenControls(c);
}

}
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 -
confirmed.

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";
ddl.Items.Add(itm);
ListItem itmd = new ListItem();
itmd.Text = "545";
ddl.Items.Add(itmd);
ddl.AutoPostBack=true;
ddl.SelectedIndexChanged +=new EventHandler(SaveData);
this.Controls.Add(ddl);
-----

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.

Help!

Thanks in advance!

Nov 19 '05 #1
5 2224
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 :
http://msdn.microsoft.com/library/de.../viewstate.asp
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by MikeY | last post: by
2 posts views Thread by Michael Ramey | last post: by
13 posts views Thread by Chris Thunell | last post: by
4 posts views Thread by Brian Shannon | last post: by
3 posts views Thread by Leo J. Hart IV | last post: by
7 posts views Thread by msdev | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.