By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

Best design practices for user control interaction

P: n/a
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...
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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" <jh*****@securitytrustco.com> wrote in message
news:80*************************@posting.google.co m...
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...

Nov 18 '05 #2

P: n/a
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/de...singStages.asp
http://msdn.microsoft.com/library/de...nlifecycle.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Josh Harris" <jh*****@securitytrustco.com> wrote in message
news:80*************************@posting.google.co m...
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...

Nov 18 '05 #3

P: n/a
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" <jh*****@securitytrustco.com> wrote in message
news:80*************************@posting.google.co m...
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...

Nov 18 '05 #4

P: n/a
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
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.