Best design practices for user control interaction

Discussion in 'ASP .Net' started by Josh Harris, Apr 13, 2004.

  1. Josh Harris

    Josh Harris Guest

    Here is my question:

    It is common to have many pieces of business logic encapsulated within
    asp.net user controls. This can be found in high visibility projects
    such as the iBuySpy portal from MS. Virtually every bit of the site
    is encapsulated within user controls. The problem begins when you try
    to have user controls pass information to one another as well as to
    their parent form. This can be accomplished by creating public
    properties of the user controls, and then referencing those properties
    to get around the issue of the parents page load event occuring before
    the event handler in the user control (assuming that you have an event
    that needs to send information to the parent page). This is great as
    long as you can easily create a public property that can return what
    you need from the user control before its event handler fires (such as
    grabbing text from a user input text box) But what about more complex
    needs, such as a user control that contains a datagrid that a user can
    select a record which then needs to pass information from that record
    to a different user control on the same page? This can be very
    convoluted -although it is possibly to get around most of these
    issues, I'm just wondering if there are better ways to handle this
    kind of interoperability or if we are stuck with public properties for
    most applications...
    Josh Harris, Apr 13, 2004
    #1
    1. Advertising

  2. Josh Harris

    Oleg Ogurok Guest

    Check out the concept called Event Bubbling. It allows an event thrown
    inside an inner control to propagate out to the outer controls. Most
    controls make use of Control.OnBubbleEvent

    -Oleg.


    "Josh Harris" <> wrote in message
    news:...
    > Here is my question:
    >
    > It is common to have many pieces of business logic encapsulated within
    > asp.net user controls. This can be found in high visibility projects
    > such as the iBuySpy portal from MS. Virtually every bit of the site
    > is encapsulated within user controls. The problem begins when you try
    > to have user controls pass information to one another as well as to
    > their parent form. This can be accomplished by creating public
    > properties of the user controls, and then referencing those properties
    > to get around the issue of the parents page load event occuring before
    > the event handler in the user control (assuming that you have an event
    > that needs to send information to the parent page). This is great as
    > long as you can easily create a public property that can return what
    > you need from the user control before its event handler fires (such as
    > grabbing text from a user input text box) But what about more complex
    > needs, such as a user control that contains a datagrid that a user can
    > select a record which then needs to pass information from that record
    > to a different user control on the same page? This can be very
    > convoluted -although it is possibly to get around most of these
    > issues, I'm just wondering if there are better ways to handle this
    > kind of interoperability or if we are stuck with public properties for
    > most applications...
    Oleg Ogurok, Apr 13, 2004
    #2
    1. Advertising

  3. I commonly use the PreRender event of the page and controls for complex
    situations like this.
    By the time the PreRender event happens, the page is loaded and all its
    controls have received their events. So you are now in a very stateful
    moment which makes it easier to communicate among various controls.

    Of course knowledge is power. I suggest you study the lifecycle of pages
    and controls.
    Here are some good articles on the subject:
    http://www.15seconds.com/issue/020102.htm
    http://msdn.microsoft.com/library/d...on/html/vbconWebFormsPageProcessingStages.asp
    http://msdn.microsoft.com/library/d...guide/html/cpconcontrolexecutionlifecycle.asp

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net


    "Josh Harris" <> wrote in message
    news:...
    > Here is my question:
    >
    > It is common to have many pieces of business logic encapsulated within
    > asp.net user controls. This can be found in high visibility projects
    > such as the iBuySpy portal from MS. Virtually every bit of the site
    > is encapsulated within user controls. The problem begins when you try
    > to have user controls pass information to one another as well as to
    > their parent form. This can be accomplished by creating public
    > properties of the user controls, and then referencing those properties
    > to get around the issue of the parents page load event occuring before
    > the event handler in the user control (assuming that you have an event
    > that needs to send information to the parent page). This is great as
    > long as you can easily create a public property that can return what
    > you need from the user control before its event handler fires (such as
    > grabbing text from a user input text box) But what about more complex
    > needs, such as a user control that contains a datagrid that a user can
    > select a record which then needs to pass information from that record
    > to a different user control on the same page? This can be very
    > convoluted -although it is possibly to get around most of these
    > issues, I'm just wondering if there are better ways to handle this
    > kind of interoperability or if we are stuck with public properties for
    > most applications...
    Steve C. Orr [MVP, MCSD], Apr 13, 2004
    #3
  4. Josh Harris

    Jim Corey Guest

    Josh,

    I'm not familiar with IBuySpy but I've seen other complaints about how it's
    advertised as
    'best practices'. I think a lot of the apps at asp.net are good for
    different techniques but
    not neccessarily for architecting a large app.

    Typically one would want to keep business logic in classes separate from the
    controls.

    User controls can be very tricky with the situations you describe,
    especially if they're loaded dynamically (is this the case here? I spent a
    lot of time trying to get this straight.)

    You may want to use public properties of the page rather than the properties
    of the control.
    You could use event bubbling as noted in another post or do this:

    (The page in this case is MainReport.aspx)
    Dim myParent As MainReport
    myParent = CType(HttpContext.Current.Handler, MainReport)
    myParent.SomeFunction()

    The page would probably have an variable declared for other controls, so you
    wouldn't have to
    do stuff like myParent.Parent.FindControl(...

    Jim

    "Josh Harris" <> wrote in message
    news:...
    > Here is my question:
    >
    > It is common to have many pieces of business logic encapsulated within
    > asp.net user controls. This can be found in high visibility projects
    > such as the iBuySpy portal from MS. Virtually every bit of the site
    > is encapsulated within user controls. The problem begins when you try
    > to have user controls pass information to one another as well as to
    > their parent form. This can be accomplished by creating public
    > properties of the user controls, and then referencing those properties
    > to get around the issue of the parents page load event occuring before
    > the event handler in the user control (assuming that you have an event
    > that needs to send information to the parent page). This is great as
    > long as you can easily create a public property that can return what
    > you need from the user control before its event handler fires (such as
    > grabbing text from a user input text box) But what about more complex
    > needs, such as a user control that contains a datagrid that a user can
    > select a record which then needs to pass information from that record
    > to a different user control on the same page? This can be very
    > convoluted -although it is possibly to get around most of these
    > issues, I'm just wondering if there are better ways to handle this
    > kind of interoperability or if we are stuck with public properties for
    > most applications...
    Jim Corey, Apr 13, 2004
    #4
  5. Josh Harris

    Josh Harris Guest

    Ok, first thing, I have no idea why I said it is common to encapsulate
    business login in a user control, I did not mean to say that in any
    way, what I meant to say is that it is very usefull to be able to have
    user controls talk to one another, such as one user control takes user
    input, and another displays information based on what the user
    entered...


    Jim, Yes the controls are added dynamically.

    Steve, I can't user page_prerender in the parent page when trying to
    pass information from a user control to another user control. (but
    thanks for the links)

    I have also encountered a new *feature*, apparently when using any
    type of caching on a user control, output caching, fragment caching,
    etc, you can no longer find the control using the findcontrol method.
    I realize that this is because when the control is cached it is
    removed from the pages control collection (although I'm not convinced
    that is the best architecture) but then where IS the control placed?
    how can I easily access a cached control with a findcontrol sort of
    method?

    Thanks,
    Josh
    Josh Harris, Apr 17, 2004
    #5
    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. noname
    Replies:
    0
    Views:
    538
    noname
    Jun 26, 2003
  2. =?Utf-8?B?TmF0aGFuVg==?=

    User Controls/Design Best Practices

    =?Utf-8?B?TmF0aGFuVg==?=, Oct 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    421
    DWinter
    Oct 2, 2004
  3. David Bowey
    Replies:
    1
    Views:
    439
    Steve C. Orr [MVP, MCSD]
    Mar 16, 2005
  4. David Bowey

    Best Practices: Porting ASCX control to compiled Custom Control?

    David Bowey, Mar 16, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    172
    Steve C. Orr [MVP, MCSD]
    Mar 16, 2005
  5. Kenneth Baltrinic

    Custom Server Control Design Best Practices?

    Kenneth Baltrinic, Apr 27, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    265
    Teemu Keiski
    Apr 29, 2006
Loading...

Share This Page