Access ASCX properties/methods from another ASCX file

D

djscratchnsniffing

i know you can access an ascx's properties/methods from an aspx file.
Let's say you have an aspx file with two code-behind files(ascx files).
Can you access one of the ascx file's properties/methods from the other
ascx file?
 
J

John Saunders

i know you can access an ascx's properties/methods from an aspx file.
Let's say you have an aspx file with two code-behind files(ascx files).
Can you access one of the ascx file's properties/methods from the other
ascx file?

You could, if the other .ascx file knew that the first one was there. But
why should it know that the other one is there?

You should try to make your user controls independant of each other, so that
they can be used in various combinations. You should allow the page that the
user controls are on to mediate between them (this is similar to the
Mediator Pattern). Have the user controls expose properties and events. The
page can set and read the properties, and can respond to the events.
Certainly, the page could set a property in one control based on the value
of a property in another control.

This way, each user control just does its own thing, and coordination for a
particular purpose is performed by the page.

The only exception to this is when you have a set of user controls which are
frequently used together to perform the same task. The solution is to create
a user control to contain this set of user controls. Let the container user
control be the Mediator of the controls it contains. The container might
have properties and events which were originally the properties and events
of its contained controls, but that's just fine:

Container.ascx.vb:

Protected WithEvents _innerControlA As InnerControlA

Public Property Username As String
Get
Return _innerControlA.Username
End Get
Set(ByVal Value As String)
_innerControlA.Username = Value
End Set
End Property

This way, the individual user controls in the Container are still
independant of each other, and one won't have to be changed if the other
changes. Only the Container would have to change, and the Page itself might
not even have to change.

John Saunders
 
N

Nicole Schenk

i know you can access an ascx's properties/methods from an aspx file.
Let's say you have an aspx file with two code-behind files(ascx files).
Can you access one of the ascx file's properties/methods from the other
ascx file?
I don't see why not if you have a reference to the instances of classes in
the files
 
D

djscratchnsniffing

thanks for all of the help. i'll read the tutorial information. im sure
that will help.
im new to asp.net development. most of the stuff ive done is in asp.
i guess what im trying to do have a main aspx page with a few ascx
files (code-behind). Ascx A is where the user enters information. Ascx
B displays the results based upon the info entered into ascx A.
thanks again for the good leads. any assistance is always appreciated.
 
M

Mr Not So Know It All

thanks for all of the help. i'll read the tutorial information. im sure
that will help.
im new to asp.net development. most of the stuff ive done is in asp.
i guess what im trying to do have a main aspx page with a few ascx
files (code-behind). Ascx A is where the user enters information. Ascx
B displays the results based upon the info entered into ascx A.
thanks again for the good leads. any assistance is always appreciated.
 

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

Top