Calendar Controls don't fire events

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Calendar Controls don't fire events

    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 Guest

  2. Similar Questions and Discussions

    1. DataList doesn't fire events
      Hi, I'm new to this today, and I've got some test code (see below.) The data loads fine. I can't understand why any of the events fire. Once...
    2. 2 controls on the same page and wrong events fire
      I have a button control that uses the <button> tag. I use the render control method. I have the name attribute set to the control unique id. ...
    3. Events doesn't fire up for buttons in a web usercontrol - Help
      Hi I have a main page called lobby.aspx. lobby have the main menu. below that I loads web user controls (ascx) according to the selected...
    4. Why do my events not fire?
      Not to worry - discovered the problem. I was changing the children control's ID's before render but after init. so the controls that were generating...
    5. how to fire events of dropdownlist in datagrid
      Hi guys, This is a subtle one. I'm looking for someone who can give me some insight relative to eventsfiring in the codebehind. I have...
  3. #2

    Default Re: Calendar Controls don't fire events

    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 Guest

  4. #3

    Default Re: Calendar Controls don't fire events

    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 Guest

  5. #4

    Default Calendar Controls don't fire events

    This is best one article so far I have read online. I would like to appreciate you for making it very simple and easy. I have found another nice post related to this post over the internet which also explained very well.
    Thanks Everyone for your precious post!!
    Tanuj Kumar Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139