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

M

mytestemailaccount

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.AppSettings["XSLT"];
string sControls =
System.Configuration.ConfigurationSettings.AppSettings[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.WebControls.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!
 
M

mytestemailaccount

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!
 
M

mytestemailaccount

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!
 
M

mytestemailaccount

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top