User Control and Page Events

Discussion in 'ASP .Net' started by =?Utf-8?B?VDhS?=, Mar 14, 2007.

  1. Ok...I've been looking for an answer to this problem and can't seem to find
    one...Framework 1.1 mind you.

    I have a base class that inherits from UserControl. I have 5 and soon to be
    12 user controls that derive from this base class that represent US States
    with appropriate business logic and rules. Inside the base class is an event
    that is raised whenever you click a checkbox in the datagrid of each of the
    user controls...no biggie... This event is to be seen by the page so that it
    can take appropriate action upon handling the event (such as disabling
    selections, etc...) however the page cannot see this event to handle it.

    Now another event needs to be fired by the page when you make a selection in
    one or more dropdowns that are then handled by each of the user controls as
    appropriate. For example, if one dropdown value gets selected, each state
    user control will compare its business rules to the selection and uncheck a
    checkbox in its grid if the rules dictate. However, this event is never seen
    by the control.

    My problem is that I don't know where to define these events so that the
    page and the controls can see and use them. The UC event i've defined in the
    baseclass so that all the derived controls can access it through a protected
    method. However, I cannot construct a handles on the page because the page
    can't see it. Likewise I cannot construct a handles in the controls because
    they cannot see the page event.

    Any ideas or need for further info?
     
    =?Utf-8?B?VDhS?=, Mar 14, 2007
    #1
    1. Advertising

  2. >Likewise I cannot construct a handles in the controls because
    >they cannot see the page event.
    > Any ideas or need for further info?


    What about this idea:

    1. You define a Notification List (NL) on the page for each event you need
    to handle (or one list for all events):

    public ArrayList NotificationList = new ArrayList();

    2. Each UC has one base class, wich in its OnLoad method registers itself in
    the NL:

    public class MyBaseControl: UserControl
    {
    override OnLoad()
    {
    if (Page != null) (Page as MyBasePage).NotificationList.Add(this);
    }

    public HandlePageEvent(EventArgs e)
    {
    DoSomething();
    }
    }

    3. When you have an event on a page, you do something like this:

    protected MyList_SelectedIndexChanged(EventArgs e)
    {
    foreach (MyBaseControl control in NotificationList)
    control.HandlePageEvent(e);
    }

    So, all your UC will see the page's events.

    PS. I typed directly into this window, so, it is a "psevdocode".
     
    Sergey Gorbachev, Mar 14, 2007
    #2
    1. Advertising

  3. That is what I wanted to avoid actually. But it may be the only way I can
    achieve what I want to do. I do similar stuff elsewhere but it doesn't seem
    as elegant as just doing it all with events and not making methods that
    iterate over all the controls when each control could just subscribe the the
    event as needed.

    I would rather do this with pure events. It seems that if you don't derive
    from a base class then event handling is 1:1. However, as soon as I started
    deriving then the whole event model blew up because nothing can see the
    other's events. Which seems counterintuitive.

    Thanks for the input - any other ideas?

    "Sergey Gorbachev" wrote:

    > >Likewise I cannot construct a handles in the controls because
    > >they cannot see the page event.
    > > Any ideas or need for further info?

    >
    > What about this idea:
    >
    > 1. You define a Notification List (NL) on the page for each event you need
    > to handle (or one list for all events):
    >
    > public ArrayList NotificationList = new ArrayList();
    >
    > 2. Each UC has one base class, wich in its OnLoad method registers itself in
    > the NL:
    >
    > public class MyBaseControl: UserControl
    > {
    > override OnLoad()
    > {
    > if (Page != null) (Page as MyBasePage).NotificationList.Add(this);
    > }
    >
    > public HandlePageEvent(EventArgs e)
    > {
    > DoSomething();
    > }
    > }
    >
    > 3. When you have an event on a page, you do something like this:
    >
    > protected MyList_SelectedIndexChanged(EventArgs e)
    > {
    > foreach (MyBaseControl control in NotificationList)
    > control.HandlePageEvent(e);
    > }
    >
    > So, all your UC will see the page's events.
    >
    > PS. I typed directly into this window, so, it is a "psevdocode".
    >
    >
    >
     
    =?Utf-8?B?VDhS?=, Mar 15, 2007
    #3
  4. > Thanks for the input - any other ideas?

    Maybe the UC can add their own event handlers to the Page's controls?
    Something like this:

    public class MyBaseControl: UserControl
    {
    override OnInit()
    {
    base.OnInit();

    if (Page != null) (Page.MyList.SelectedIndexChanged += new
    EventHandler(MyList_SelectionChanged);
    }

    void MyList_SelectionChanged(object sender, EventArgs e)
    {
    DoSomething();
    }
    }
     
    Sergey Gorbachev, Mar 15, 2007
    #4
    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. shiv
    Replies:
    0
    Views:
    482
  2. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    562
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  3. Joe Iano

    user control and order of events in page

    Joe Iano, Jul 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    225
    Joe Iano
    Jul 7, 2003
  4. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    375
    Chris
    Aug 30, 2005
  5. Replies:
    1
    Views:
    159
    Peter Michaux
    Apr 27, 2007
Loading...

Share This Page