By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
Where's your AddEvent method called?

Nov 19 '05 #2

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.