Best design practices for user control interaction

J

Josh Harris

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...
 
O

Oleg Ogurok

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.
 
S

Steve C. Orr [MVP, MCSD]

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
 
J

Jim Corey

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
 
J

Josh Harris

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top