Re: Attaching Custom Events for WebUserControl

Discussion in 'ASP .Net' started by Guest, Dec 13, 2004.

  1. Guest

    Guest Guest

    I guess I did not state my real question clearly enough.
    Is one supposed to be responsible for creating the attribute and
    postback refreence. I had already done what was reiterated above and while
    the event was declared no attribute was created for the postback.
    "John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
    news:...
    > "Ollie" <> wrote in message
    > news:...
    >> if you want server side event for your ascx control then you need to
    >> define
    >> a delegate and then define an event
    >> e.g.
    >>
    >> public testControl : System.Web.UI.UserControls
    >> {
    >> public delegate void SelectionDelegate(int selected);
    >> public event SelectionDelegate SelectionChanged();
    >>
    >> public void Page_Load()
    >> {
    >> .....
    >> }
    >>
    >> private void FireSelectionEvent(int selection)
    >> {
    >> if(SelectionChanged != null)
    >> SelectionChanged(selection)
    >> }
    >> }

    >
    >
    > There is a generally-accepted pattern for events in .NET:
    >
    > 1) Decide on a name for the event, let's say, "SelectionChanged"
    > 2) Decide what information needs to be passed to listeners when the event
    > fires. Let's say, an integer "selection"
    > 3) Define a derived class of EventArgs to hold this information. The class
    > should accept the information in its constructor, and should define a
    > read-only property to access the information: The name of the class should
    > be the name of the event followed by "EventArgs":
    >
    > public class SelectionChangedEventArgs : EventArgs
    > {
    > private int _selection;
    > public SelectionChangedEventArgs(int selection) : base()
    > {
    > _selection = selection;
    > }
    >
    > public int Selection
    > {
    > get {return _selection;}
    > }
    > }
    >
    > 4) Define a delegate type for the event. The name of the type should be
    > the name of the event followed by "EventHandler". It should return void
    > and take two parameters. The first should be called "sender" and should be
    > of type "object". The second should be called "e" and should be of your
    > EventArgs type:
    >
    > public delegate void SelectionChangedEventHandler(object sender,
    > SelectionChangedEventArgs e);
    >
    > 5) Define the event:
    >
    > public event SelectionChangedEventHandler SelectionChanged;
    >
    > 6) Define a method to fire the event. It should be protected virtual in
    > order to permit derived classes to handle the event efficiently, and to
    > allow them to raise it in a different manner. The method should be named
    > "On" followed by the name of the event. It should accept a single
    > parameter named "e", of your EventArgs type:
    >
    > protected virtual void OnSelectionChanged(SelectionChangedEventArgs e)
    > {
    > if (SelectionChanged != null)
    > {
    > SelectionChanged(this, e);
    > }
    > }
    >
    > 7) For "extra credit", you can define "convenience" overloads of your "On"
    > method. They should _not_ be virtual, but should call the virtual version:
    >
    > protected void OnSelectionChanged(int selection)
    > {
    > OnSelectionChanged(new SelectionChangedEventArgs(selection));
    > }
    >
    > 8) Fire the event as required:
    >
    > private void ddl_SelectedIndexChanged(object sender, EventArgs e)
    > {
    > OnSelectionChanged(ddl.SelectedIndex);
    > }
    >
    >
    > John Saunders
    >
    >
    >> <> wrote in message
    >> news:%23epf$...
    >>> I am wondering what the best method of attaching custom Events to

    >> custom
    >>> WebUserControls are. I cannot seem to find the proper terminology to

    >> expand
    >>> my research.
    >>> Basicallly I have a custom user control that has 2 or 3 events
    >>> selectionChanged
    >>> DropDownOpened
    >>>
    >>> I would like the user to be able to attach Server Side events for both.
    >>> I
    >>> have the client side implementation worked out. I just need to find a

    >> proper
    >>> way to to get everything registered so that the output produces
    >>> something
    >>> like
    >>> <div selectionChanged="__dopostback('contorolname', '');"
    >>> dropdownOpened="__dopostback('controlname', 'dropDownOepned????');">
    >>> ....</div>
    >>>
    >>> Any help or pointers would be greatly appreciated. Thanks in Advance
    >>>
    >>>

    >>
    >>

    >
    >
    Guest, Dec 13, 2004
    #1
    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. AC
    Replies:
    0
    Views:
    358
  2. smith Smith

    attaching events to textbox in a grid....

    smith Smith, May 27, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    732
    Peter Blum
    May 29, 2004
  3. Ollie
    Replies:
    0
    Views:
    403
    Ollie
    Dec 13, 2004
  4. Terry Olsen

    WebUserControl Events & Properties?

    Terry Olsen, Sep 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    527
    Terry Olsen
    Sep 7, 2006
  5. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

    Kasabaarde Sumta, Dec 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    177
    Kasabaarde Sumta
    Dec 13, 2004
Loading...

Share This Page