Global Events that all instances of a control handles

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

  1. #1

    Default Global Events that all instances of a control handles

    I have a user control (called MyControl that inherits from
    CompositeControl). This control will raise an event when the user changes
    the color of the control. (MyControl.ColorChange). Inside the code for
    MyControl, I also listen and handle the event (ColorChange). - This actually
    implements the change in color. This works OK for the control that raised
    the event.

    However, in the aspx form, I have 5 instances of the same user control
    (MyControl). When one of the controls change color (based on user input) I
    would like to have each instance of the MyControl listen for the event and
    handle the ChangeColor event. When I try this only the control that raised
    the event will hear the event. Is there any way to globally raise an event
    that all controls could listen too?

    Thanks

    Phil



    Phil Guest

  2. Similar Questions and Discussions

    1. Odd web control problem. Two instances of control A on different pages display each other's data!
      I am currently using the ScottWatter (or ScottWater) Amazon Book Control in a website I am developing. Basically this is the outline: - It is a...
    2. Notify child control of events in parent control
      I have a parent control that has a imagebutton on it, with code to handle the click event. I also have various ascx controls that are loaded by the...
    3. Under what context Application events run in Global.asax
      (I am running .NET Framework 1.1) I have a timer(System.Timers.Timer) running in my Global.asax page. In the timer.elapsed event I am trying to...
    4. Multiple instances of a control? <img> ??
      Is it possible to have multiple instances of a control and still access all of them in codebehind? For example, I need to be able to change an...
    5. Name of events in Global.asax
      Hi. Reading the docs about the Global.asax events I noticed the names used on the help are not the same on the file. For example, the docs talk...
  3. #2

    Default Global Events that all instances of a control handles

    I have a user control (called MyControl that inherits from
    CompositeControl). This control will raise an event when the user changes
    the color of the control. (MyControl.ColorChange). Inside the code for
    MyControl, I also listen and handle the event (ColorChange). - This actually
    implements the change in color. This works OK for the control that raised
    the event.

    However, in the aspx form, I have 5 instances of the same user control
    (MyControl). When one of the controls change color (based on user input) I
    would like to have each instance of the MyControl listen for the event and
    handle the ChangeColor event. When I try this only the control that raised
    the event will hear the event. Is there any way to globally raise an event
    that all controls could listen too?

    Thanks

    Phil




    Phil Guest

  4. #3

    Default Re: Global Events that all instances of a control handles

    On 2006-09-11 02:03:52 -0500, "Phil" <lynch_15@hotmail.com> said:
    > I have a user control (called MyControl that inherits from
    > CompositeControl). This control will raise an event when the user
    > changes the color of the control. (MyControl.ColorChange). Inside the
    > code for MyControl, I also listen and handle the event (ColorChange). -
    > This actually implements the change in color. This works OK for the
    > control that raised the event.
    >
    > However, in the aspx form, I have 5 instances of the same user control
    > (MyControl). When one of the controls change color (based on user
    > input) I would like to have each instance of the MyControl listen for
    > the event and handle the ChangeColor event. When I try this only the
    > control that raised the event will hear the event. Is there any way to
    > globally raise an event that all controls could listen too?
    >
    > Thanks
    >
    > Phil
    >
    In the user control
    * Define a Delegate
    * Define an event based on the delegate
    * Fire the event

    In the "listener" controls
    * subscribe to the event
    * write an event handler
    * "wire" the event handler to the delegate

    /*************** Event Publisher ****************/
    public PublisherClass { // the class that changed it's color
    pubic delegate void myDelegate(string newColor);
    public event myDelegate myEvent;

    // "wire" this method to the OnChange property of your Select listbox.
    private void OnColorChange(string theNewColor) {
    this.myEvent();
    }
    }


    /*********** meanwhile, in the listener contols' code ***********/
    public ListenerClass {
    PublisherClass myPublisher = new PublisherClass();

    override protected void OnInit (EventArgs e) {
    myPublisher.myEvent += myPublisher.myDelegate(ChangeMyColor);
    }

    public void ChangeMyColor (string newColor) {
    // change the color to newColor
    }
    }

    /************ Caveat **************/
    If you want every control to listen to every other control's
    ChangeColor event then I'm not sure
    how to neatly implement that. I guess every control will be both a
    publisher and subscriber - sounds cool, if it works - Either every
    control defines it's own event and all controls subscribe to all other
    control's events.. OR .. here's something wacky: think it *could* go
    like this (WARNING: wild-ass guess follows):
    * Define the delegate and event at the namespace level (i.e. at the
    container page level?)
    * Each control will have an OnChange handler for what I assume is a
    drop-down select list of colors
    * In that OnChange handler of every control, call the event
    * in OnInit() of every control set "this.myEvent +=
    this.myDelegate(ChangeMyColor)" (not too sure that "this" will cut it)
    * so the drop-down list event handler fires the event and a separate
    method (wired to the delegate) does the actual work
    ***** would this cause an infinite loop?


    RLJ Guest

  5. #4

    Default Re: Global Events that all instances of a control handles

    Thanks for the response. I managed a workaround, but your suggestions is
    much neater. Will try to implement it this way!


    "RLJ" <robert@joneshouse.com> wrote in message
    news:2006100101400043658-robert@joneshousecom...
    > On 2006-09-11 02:03:52 -0500, "Phil" <lynch_15@hotmail.com> said:
    >
    >> I have a user control (called MyControl that inherits from
    >> CompositeControl). This control will raise an event when the user
    >> changes the color of the control. (MyControl.ColorChange). Inside the
    >> code for MyControl, I also listen and handle the event (ColorChange). -
    >> This actually implements the change in color. This works OK for the
    >> control that raised the event.
    >>
    >> However, in the aspx form, I have 5 instances of the same user control
    >> (MyControl). When one of the controls change color (based on user input)
    >> I would like to have each instance of the MyControl listen for the event
    >> and handle the ChangeColor event. When I try this only the control that
    >> raised the event will hear the event. Is there any way to globally raise
    >> an event that all controls could listen too?
    >>
    >> Thanks
    >>
    >> Phil
    >>
    > In the user control
    > * Define a Delegate
    > * Define an event based on the delegate
    > * Fire the event
    >
    > In the "listener" controls
    > * subscribe to the event
    > * write an event handler
    > * "wire" the event handler to the delegate
    >
    > /*************** Event Publisher ****************/
    > public PublisherClass { // the class that changed it's color
    > pubic delegate void myDelegate(string newColor);
    > public event myDelegate myEvent;
    >
    > // "wire" this method to the OnChange property of your Select
    > listbox.
    > private void OnColorChange(string theNewColor) {
    > this.myEvent();
    > }
    > }
    >
    >
    > /*********** meanwhile, in the listener contols' code ***********/
    > public ListenerClass {
    > PublisherClass myPublisher = new PublisherClass();
    >
    > override protected void OnInit (EventArgs e) {
    > myPublisher.myEvent += myPublisher.myDelegate(ChangeMyColor);
    > }
    >
    > public void ChangeMyColor (string newColor) {
    > // change the color to newColor
    > }
    > }
    >
    > /************ Caveat **************/
    > If you want every control to listen to every other control's ChangeColor
    > event then I'm not sure
    > how to neatly implement that. I guess every control will be both a
    > publisher and subscriber - sounds cool, if it works - Either every control
    > defines it's own event and all controls subscribe to all other control's
    > events.. OR .. here's something wacky: think it *could* go like this
    > (WARNING: wild-ass guess follows):
    > * Define the delegate and event at the namespace level (i.e. at the
    > container page level?)
    > * Each control will have an OnChange handler for what I assume is a
    > drop-down select list of colors
    > * In that OnChange handler of every control, call the event
    > * in OnInit() of every control set "this.myEvent +=
    > this.myDelegate(ChangeMyColor)" (not too sure that "this" will cut it)
    > * so the drop-down list event handler fires the event and a separate
    > method (wired to the delegate) does the actual work
    > ***** would this cause an infinite loop?
    >
    >

    Phil 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