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

Discussion in 'ASP .Net' started by mytestemailaccount@gmail.com, Mar 23, 2005.

  1. Guest

    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!
     
    , Mar 23, 2005
    #1
    1. Advertising

  2. Where's your AddEvent method called?
     
    Guillaume Davion, Mar 23, 2005
    #2
    1. Advertising

  3. Guillaume Davion, Mar 23, 2005
    #3
  4. Guest

    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!
     
    , Mar 23, 2005
    #4
  5. Guest

    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!
     
    , Mar 23, 2005
    #5
  6. Guest

    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!
     
    , Mar 23, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven D'Aprano

    Why are "broken iterators" broken?

    Steven D'Aprano, Sep 21, 2008, in forum: Python
    Replies:
    8
    Views:
    693
  2. Cameron Simpson

    Re: Why are "broken iterators" broken?

    Cameron Simpson, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    614
    Cameron Simpson
    Sep 22, 2008
  3. Faizan Ahmed
    Replies:
    1
    Views:
    308
    John Saunders
    Jan 4, 2005
  4. Faizan Ahmed
    Replies:
    1
    Views:
    388
    John Saunders
    Jan 4, 2005
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    420
    Chris
    Aug 30, 2005
Loading...

Share This Page