Calendar Controls don't fire events

Discussion in 'ASP .Net Building Controls' started by Mark Sisson, Nov 29, 2003.

  1. Mark Sisson

    Mark Sisson Guest

    Good afternoon all.
    I am creating a usercontrol (*.ascx) with a Calendar control on it.
    In the Calendar's DayRender event I'm dynamically adding ImageButton
    controls to cells on the calendar. For each of these image button I'm
    hooking up a click event as so:

    imgButton.Click += new ImageClickEventHandler(this.ImageButton_Click);

    Problem is that when the user clicks on the image button my event
    handler never fires. Now I think I know the reason but I don't know
    the best solution. Since the controls are added too late in the page
    lifecycle they aren't available when the postback happens so they
    never get called. So how do I create all these ImageButton's so that
    they can receive events on post back?

    I've heard about bubbling events but I'm not sure how they work.
    Could I a add control to my Calendar in it's DayRender event and tell
    the Framework to have it's click-event be sent to the Calendar's click
    event???????

    tia
     
    Mark Sisson, Nov 29, 2003
    #1
    1. Advertising

  2. Mark Sisson

    Jos Guest

    Mark Sisson wrote:
    > Good afternoon all.
    > I am creating a usercontrol (*.ascx) with a Calendar control on it.
    > In the Calendar's DayRender event I'm dynamically adding ImageButton
    > controls to cells on the calendar. For each of these image button I'm
    > hooking up a click event as so:
    >
    > imgButton.Click += new ImageClickEventHandler(this.ImageButton_Click);
    >
    > Problem is that when the user clicks on the image button my event
    > handler never fires. Now I think I know the reason but I don't know
    > the best solution. Since the controls are added too late in the page
    > lifecycle they aren't available when the postback happens so they
    > never get called. So how do I create all these ImageButton's so that
    > they can receive events on post back?
    >
    > I've heard about bubbling events but I'm not sure how they work.
    > Could I a add control to my Calendar in it's DayRender event and tell
    > the Framework to have it's click-event be sent to the Calendar's click
    > event???????
    >
    > tia


    I would suggest that on postback, you recreate the calendar in
    exactly the same way as before the postback.
    Then the events will fire normally.
    In the event handler, you can destroy everything again and
    build the final page.

    --

    Jos
     
    Jos, Dec 1, 2003
    #2
    1. Advertising

  3. Mark Sisson

    Mark Sisson Guest

    Figured it out gang. Basically it comes down to this: GIVE ALL YOUR
    CONTROLS IDS!!

    Basically I was creating a control dynamically in the PreRender event
    and then recreating it during Postback in the Init event like this:

    Pseudocode
    ===================
    Page_OnInit
    if (Session["ctrlname"] == "") Session["ctrlname"] = "a.ascx";
    UserControl uc = LoadControl(Session["ctrlname"];
    ud.ID = "MyControl";
    Controls.Add(uc)

    Page_PreRender
    switch case (Session["ctrlname"]) {
    case "a.ascx" {
    Session["ctrlname"] = "b.ascx";
    break;
    }
    case "b.ascx" {
    Session["ctrlname"] = "c.ascx";
    break;
    }
    }
    Controls.Clear();
    Controls.Add(LoadControl(Session["ctrlname"])



    The problem this causes is that when you Clear() the Controls array it
    doesn't reset it's control id numbering scheme to start back at zero.
    So the first time this page is rendered the controls that goes "out
    the door" has an id of ctrl_1 NOT ctrl_0. So during Postback when we
    do a great job of recreating the control in the Init routine the
    control we create has the id ctrl_0 and doesn't match the control
    being posted back which is ctrl_1.

    The whole solution is to hardcode an ID for your control. Here's the
    new pseudocode that now works:


    Pseudocode
    ===================
    Page_OnInit
    if (Session["ctrlname"] == "") Session["ctrlname"] = "a.ascx";
    Controls.Add(LoadControl(Session["ctrlname"])

    Page_PreRender
    switch case (Session["ctrlname"]) {
    case "a.ascx" {
    Session["ctrlname"] = "b.ascx";
    break;
    }
    case "b.ascx" {
    Session["ctrlname"] = "c.ascx";
    break;
    }
    }
    Controls.Clear();
    UserControl uc = LoadControl(Session["ctrlname"];
    ud.ID = "MyControl";
    Controls.Add(uc)
     
    Mark Sisson, Dec 3, 2003
    #3
    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. Kenneth Baltrinic
    Replies:
    1
    Views:
    509
    bruce barker
    May 31, 2007
  2. shapper

    Events don't fire?!

    shapper, Jun 20, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    401
    =?Utf-8?B?U3JpcmFtIFN1cmFwdXJlZGR5?=
    Jun 20, 2007
  3. 2 controls on the same page and wrong events fire

    , Jun 2, 2005, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    169
  4. Mark Sisson

    Calendar Controls don't fire events

    Mark Sisson, Nov 29, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    152
    Mark Sisson
    Dec 3, 2003
  5. Nico
    Replies:
    0
    Views:
    137
Loading...

Share This Page