Ask a Question related to ASP.NET Building Controls, Design and Development.
-
Michael #1
Child user control accessing parent properties
We have a user control (Titlebar) that loads other user controls (children)
into itself based on a property set in the HTML:
<fss:Titlebar id="empStmtListing" runat="server"
UserControl="StatementListing.ascx" Width="90"
HelpId="1" ListingType="3"></fss:Titlebar>
Some of the children user controls need properties set to execute. We would
put these properties in the HTML of the parent,
for example, the ListingType property above. Is there anyway that
StatementListing.ascx can access and read the ListingType
property? Or will our Titlebar user control always have to pass the
properties into the child control?
Thanks
--
Michael W. Wilson
Raytheon Company
Sr. Business Programmer
Michael Guest
-
Composite Control and accessing complex properties of child controls - C# VS2005
Hello, I am trying to build a composite control that contains a few objects, in particular a calendar and a textbox. I have the bulk of the... -
Parent/Child relations - Trying to access child control for save
I have a parent datagrid that has my customer information. For each customer I have a child datagrid with all their part information. In the... -
User Control - Accessing Properties from the Container Page
Dear all, Please check my problem - Problem: I have created a User Control(UC1) for navigating between pages. There are 4 buttons on the user... -
Setting Properties on a user control on from the Parent Page
"Eric Elliston" <eelliston@neighborhoodamerica.com> wrote in message news:OiPSIL2ZDHA.1492@TK2MSFTNGP12.phx.gbl... All and control end the -
Access properties of parent page from user control
I am trying to build a control that can only be used on a page that inherits from a custom class. This base class as a series of public... -
John Saunders #2
Re: Child user control accessing parent properties
"Michael" <mwilson@nospam.raytheon.com> wrote in message
news:o0WIc.5$5c3.0@dfw-service2.ext.ray.com...(children)> We have a user control (Titlebar) that loads other user controlswould> into itself based on a property set in the HTML:
>
> <fss:Titlebar id="empStmtListing" runat="server"
> UserControl="StatementListing.ascx" Width="90"
> HelpId="1" ListingType="3"></fss:Titlebar>
>
> Some of the children user controls need properties set to execute. WeIt's really bad form for a child control to know too much about its parent.> put these properties in the HTML of the parent,
> for example, the ListingType property above. Is there anyway that
> StatementListing.ascx can access and read the ListingType
> property? Or will our Titlebar user control always have to pass the
> properties into the child control?
Instead, let the parent tell the child what it needs to know.
--
John Saunders
johnwsaundersiii at hotmail
John Saunders Guest
-
XicoLoKo #3
Re: Child user control accessing parent properties
Another approach would be to have a base usercontrol class, lets say TitleBarChildControlBase, that exposes those properties from it's parent.
User controls that inherit TitleBarChildControlBase would be used only inside the TitleBar.
- xicoloko -
"John Saunders" wrote:
> "Michael" <mwilson@nospam.raytheon.com> wrote in message
> news:o0WIc.5$5c3.0@dfw-service2.ext.ray.com...> (children)> > We have a user control (Titlebar) that loads other user controls> would> > into itself based on a property set in the HTML:
> >
> > <fss:Titlebar id="empStmtListing" runat="server"
> > UserControl="StatementListing.ascx" Width="90"
> > HelpId="1" ListingType="3"></fss:Titlebar>
> >
> > Some of the children user controls need properties set to execute. We>> > put these properties in the HTML of the parent,
> > for example, the ListingType property above. Is there anyway that
> > StatementListing.ascx can access and read the ListingType
> > property? Or will our Titlebar user control always have to pass the
> > properties into the child control?
> It's really bad form for a child control to know too much about its parent.
> Instead, let the parent tell the child what it needs to know.
> --
> John Saunders
> johnwsaundersiii at hotmail
>
>
>XicoLoKo Guest
-
John Saunders #4
Re: Child user control accessing parent properties
"XicoLoKo" <XicoLoKo@discussions.microsoft.com> wrote in message
news:BFE10C5D-F059-4D73-92A1-B0A51698926C@microsoft.com...TitleBarChildControlBase, that exposes those properties from it's parent.> Another approach would be to have a base usercontrol class, lets sayinside the TitleBar.>
> User controls that inherit TitleBarChildControlBase would be used only
Ok, I originally read the OP as asking a question about user controls in
general. If, instead, the user controls in question will _always_ be used
inside of the same parent control, then knowing about their parent would be
ok.
In this case, it would be ok if the user controls were to cast their Parent
property to the type of the parent:
VB.NET:
Dim tbParentTitlebar As Titlebar = DirectCast(Parent, GetType(Titlebar))
' Can now use tbParentTitlebar.ListingType
C#:
Titlebar tbParentTitlebar = (Titlebar) Parent;
// Can now use tbParentTitlebar.ListingType
--
John Saunders
johnwsaundersiii at hotmail
parent.> "John Saunders" wrote:
>> > "Michael" <mwilson@nospam.raytheon.com> wrote in message
> > news:o0WIc.5$5c3.0@dfw-service2.ext.ray.com...> > (children)> > > We have a user control (Titlebar) that loads other user controls> > would> > > into itself based on a property set in the HTML:
> > >
> > > <fss:Titlebar id="empStmtListing" runat="server"
> > > UserControl="StatementListing.ascx" Width="90"
> > > HelpId="1" ListingType="3"></fss:Titlebar>
> > >
> > > Some of the children user controls need properties set to execute. We> >> > > put these properties in the HTML of the parent,
> > > for example, the ListingType property above. Is there anyway that
> > > StatementListing.ascx can access and read the ListingType
> > > property? Or will our Titlebar user control always have to pass the
> > > properties into the child control?
> > It's really bad form for a child control to know too much about its> > Instead, let the parent tell the child what it needs to know.
> > --
> > John Saunders
> > johnwsaundersiii at hotmail
> >
> >
> >
John Saunders Guest
-
Donald Welker #5
Re: Child user control accessing parent properties
Sorry, that should be Parent.aspx where it says Parent.ascx; I've already realized this won't work because (Sub)Control.ascx's parent is a Panel (not shown), but you get the idea -- what's the "right" way?
"Donald Welker" wrote:
> I have a parent web page that has dynamic child controls, some of which are nested. I would like for all of the child controls to have access to the parent's service objects so they can use the same database connection etc.
>
> Since it is possible for a control to be instantiated from either the parent page directly or from a control on the parent page, is it possible/OK for me to cast the parent Page to a Control in order to access the shared object(s)?
>
> Summarized example: Suppose Parent.aspx loads and displays SubControl.ascx either directly in a panel, or as a component of Control.ascx instead. In Parent.ascx I write:
> ...
> Friend Log as LogOps ' routines to access the log
> ...
> Then in both Control.ascx and SubControl.ascx I write:
> ...
> Dim Mom as Control = DirectCast(Parent,Control)
> Dim Log as LogOps = Mom.Log
> ...
> or do I have to somehow determine whether the parent is a page or a control?
>Donald Welker Guest
-
Donald Welker #6
Re: Child user control accessing parent properties
Sorry, that should be Parent.aspx where it says Parent.ascx; I've already realized this won't work because (Sub)Control.ascx's parent is a Panel (not shown), but you get the idea -- what's the "right" way?
"Donald Welker" wrote:
> I have a parent web page that has dynamic child controls, some of which are nested. I would like for all of the child controls to have access to the parent's service objects so they can use the same database connection etc.
>
> Since it is possible for a control to be instantiated from either the parent page directly or from a control on the parent page, is it possible/OK for me to cast the parent Page to a Control in order to access the shared object(s)?
>
> Summarized example: Suppose Parent.aspx loads and displays SubControl.ascx either directly in a panel, or as a component of Control.ascx instead. In Parent.ascx I write:
> ...
> Friend Log as LogOps ' routines to access the log
> ...
> Then in both Control.ascx and SubControl.ascx I write:
> ...
> Dim Mom as Control = DirectCast(Parent,Control)
> Dim Log as LogOps = Mom.Log
> ...
> or do I have to somehow determine whether the parent is a page or a control?
>Donald Welker Guest
-
John Saunders #7
Re: Child user control accessing parent properties
"Donald Welker" <Donald.WelkerN0$SPAM@N0$SPAM.navy.mil> wrote in message
news:F537A9BE-BD47-4E1B-AFA0-A177C8CBDA05@microsoft.com...are nested. I would like for all of the child controls to have access to> I have a parent web page that has dynamic child controls, some of which
the parent's service objects so they can use the same database connection
etc.parent page directly or from a control on the parent page, is it possible/OK>
> Since it is possible for a control to be instantiated from either the
for me to cast the parent Page to a Control in order to access the shared
object(s)?SubControl.ascx either directly in a panel, or as a component of>
> Summarized example: Suppose Parent.aspx loads and displays
Control.ascx instead. In Parent.ascx I write:control?> ...
> Friend Log as LogOps ' routines to access the log
> ...
> Then in both Control.ascx and SubControl.ascx I write:
> ...
> Dim Mom as Control = DirectCast(Parent,Control)
> Dim Log as LogOps = Mom.Log
> ...
> or do I have to somehow determine whether the parent is a page or a
Donald,
My first suggestion would be to set "Options Strict" on. You may find it
enlightening.
Second, I recommend that the parent should pass to the child anything which
is owned by the parent but required by the child. In your case, I would add
a Log property to the child controls and I'd let the parent set that
property after it instantiates the child controls. Presumably, your "Log"
object is pretty much read-only? I wouldn't want multiple child controls
changing it in an unpredictable sequence.
BTW, I wouldn't worry about sharing database connections at all. The
connections will likely be pooled, so a second, "duplicate" connection
shouldn't use many additional resources. Also, it doesn't seem right for a
child control to be using the parent's connection, in effect taking it over
from the parent. The child could then do things "behind the parent's back",
which is not a good idea.
--
John Saunders
johnwsaundersiii at hotmail
John Saunders Guest
-
Donald Welker #8
Re: Child user control accessing parent properties
The log object in this case is a set of shim routines to write to the NT event log, so it's not much of a risk at the moment. I'm writing a "wizard" to help build a complex document and have broken up sections into about a dozen web controls, each with it's own edit and display panel. When the user is done the final document is recorded in a database and presented for printing, so each control is supposed to be smart enough to edit its portion of the draft document and display its fields in the final version. My original thought was to write one module to communicate with the database and share it throughout, but it occurs to me that the controls will have different data adapters and don't need access to each others' data -- but I need to make sure the connection string is shared.
What I've now done is created a "template" control from which to derive the others, but now the derived controls won't load in the designer: "Abstract Class" -- do I need to add a reference to the template or can I no longer use the designer?
"John Saunders" wrote:
> "Donald Welker" <Donald.WelkerN0$SPAM@N0$SPAM.navy.mil> wrote in message
> news:F537A9BE-BD47-4E1B-AFA0-A177C8CBDA05@microsoft.com...> are nested. I would like for all of the child controls to have access to> > I have a parent web page that has dynamic child controls, some of which
> the parent's service objects so they can use the same database connection
> etc.> parent page directly or from a control on the parent page, is it possible/OK> >
> > Since it is possible for a control to be instantiated from either the
> for me to cast the parent Page to a Control in order to access the shared
> object(s)?> SubControl.ascx either directly in a panel, or as a component of> >
> > Summarized example: Suppose Parent.aspx loads and displays
> Control.ascx instead. In Parent.ascx I write:> control?> > ...
> > Friend Log as LogOps ' routines to access the log
> > ...
> > Then in both Control.ascx and SubControl.ascx I write:
> > ...
> > Dim Mom as Control = DirectCast(Parent,Control)
> > Dim Log as LogOps = Mom.Log
> > ...
> > or do I have to somehow determine whether the parent is a page or a
>
> Donald,
>
> My first suggestion would be to set "Options Strict" on. You may find it
> enlightening.
>
> Second, I recommend that the parent should pass to the child anything which
> is owned by the parent but required by the child. In your case, I would add
> a Log property to the child controls and I'd let the parent set that
> property after it instantiates the child controls. Presumably, your "Log"
> object is pretty much read-only? I wouldn't want multiple child controls
> changing it in an unpredictable sequence.
>
> BTW, I wouldn't worry about sharing database connections at all. The
> connections will likely be pooled, so a second, "duplicate" connection
> shouldn't use many additional resources. Also, it doesn't seem right for a
> child control to be using the parent's connection, in effect taking it over
> from the parent. The child could then do things "behind the parent's back",
> which is not a good idea.
> --
> John Saunders
> johnwsaundersiii at hotmail
>
>
>Donald Welker Guest
-
John Saunders #9
Re: Child user control accessing parent properties
"Donald Welker" <Donald.WelkerN0$SPAM@N0$SPAM.navy.mil> wrote in message
news:23648D86-187A-4A9F-817A-B28F66642CFE@microsoft.com...event log, so it's not much of a risk at the moment. I'm writing a "wizard"> The log object in this case is a set of shim routines to write to the NT
to help build a complex document and have broken up sections into about a
dozen web controls, each with it's own edit and display panel. When the
user is done the final document is recorded in a database and presented for
printing, so each control is supposed to be smart enough to edit its portion
of the draft document and display its fields in the final version. My
original thought was to write one module to communicate with the database
and share it throughout, but it occurs to me that the controls will have
different data adapters and don't need access to each others' data -- but I
need to make sure the connection string is shared.the others, but now the derived controls won't load in the designer:>
> What I've now done is created a "template" control from which to derive
"Abstract Class" -- do I need to add a reference to the template or can I no
longer use the designer?
As to the connection and/or connection string, you could pass that into the
controls. Just add a Connection property of type SqlConnection (or whichever
connection type you need).
As to the "no inherited user controls" problem - that's just another
trade-off with user controls. The designer isn't clever enough to do what
you want there. If you used custom controls, you wouldn't have this problem,
but you _would_ take longer to get the project done. You can have your user
controls all inherit from a base class, but that base class can't do
anything as far as UI goes. I've done this successfully in the past.
Good Luck,
John Saunders
johnwsaundersiii at hotmail
which> "John Saunders" wrote:
>> > "Donald Welker" <Donald.WelkerN0$SPAM@N0$SPAM.navy.mil> wrote in message
> > news:F537A9BE-BD47-4E1B-AFA0-A177C8CBDA05@microsoft.com...> > > I have a parent web page that has dynamic child controls, some ofto> > are nested. I would like for all of the child controls to have accessconnection> > the parent's service objects so they can use the same databasepossible/OK> > etc.> > parent page directly or from a control on the parent page, is it> > >
> > > Since it is possible for a control to be instantiated from either theshared> > for me to cast the parent Page to a Control in order to access thewhich> > object(s)?> > SubControl.ascx either directly in a panel, or as a component of> > >
> > > Summarized example: Suppose Parent.aspx loads and displays
> > Control.ascx instead. In Parent.ascx I write:> > control?> > > ...
> > > Friend Log as LogOps ' routines to access the log
> > > ...
> > > Then in both Control.ascx and SubControl.ascx I write:
> > > ...
> > > Dim Mom as Control = DirectCast(Parent,Control)
> > > Dim Log as LogOps = Mom.Log
> > > ...
> > > or do I have to somehow determine whether the parent is a page or a
> >
> > Donald,
> >
> > My first suggestion would be to set "Options Strict" on. You may find it
> > enlightening.
> >
> > Second, I recommend that the parent should pass to the child anythingadd> > is owned by the parent but required by the child. In your case, I would"Log"> > a Log property to the child controls and I'd let the parent set that
> > property after it instantiates the child controls. Presumably, youra> > object is pretty much read-only? I wouldn't want multiple child controls
> > changing it in an unpredictable sequence.
> >
> > BTW, I wouldn't worry about sharing database connections at all. The
> > connections will likely be pooled, so a second, "duplicate" connection
> > shouldn't use many additional resources. Also, it doesn't seem right forover> > child control to be using the parent's connection, in effect taking itback",> > from the parent. The child could then do things "behind the parent's> > which is not a good idea.
> > --
> > John Saunders
> > johnwsaundersiii at hotmail
> >
> >
> >
John Saunders Guest
-
John Saunders #10
Re: Child user control accessing parent properties
"Donald Welker" <Donald.WelkerN0$SPAM@N0$SPAM.navy.mil> wrote in message
news:218FA711-1B37-4A99-BF13-9A5F1430A7F3@microsoft.com...like to do is set all their properties at the same time. It would be really> What I have now are a dozen controls that are different classes. What I'd
nice to treat them as an array or collection and loop through each of them
to set the same properties. I guess I could use the New method as an
alternative.
Donald,
If they all inherit from the same base class, then there should be no
problem in setting all their properties at the same time. In fact, you would
be able to use Reflection to find all of those controls and set their
properties at once.
Now, a different (and perhaps better) solution just struck me. Since all of
the controls will require access to the same values of the same set of
properties, it makes sense to encapsulate all of those properties in a
single class. The parent page could then create a single instance of this
class and set the properties of this single instance. Then, each control can
be passed a reference to this single instance. For example (not tested):
Public Class ControlProperties
Public Property ConnectionString As String
...
End Property
'
Public Property Log As LogOps
...
End Property
...
End Class
'
Public Class MyControlBaseType
Inherits UserControl
Public Property Properties As ControlProperties
...
End Property
End Class
' In the page:
Protected Sub Page_Load (sender As Object, e As EventArgs)
...
Dim props As ControlProperties = New ControlProperties()
props.ConnectionString = "..."
props.Log = Log ' Or whatever
'
SetCommonProperties(props, Me.Controls)
End Sub
Private Sub SetCommonProperties(props As ControlProperties, ctls As
ControlCollection)
For Each ctl As Control In ctls
If TypeOf ctl Is MyControlBaseType Then
Dim base As MyControlBaseType = DirectCast(ctl,
MyControlBaseType)
base.Properties = props
End If
'
If ctl.HasControls Then
SetCommonProperties(props, ctl.Controls)
End If
Next
End Sub
You could also choose to avoid the common base type by getting fancy with
Reflection. Instead of checking to see if the control is a
MyControlBaseType, you could check to see if it had a property named
"Properties" which was of a type which derived from "ControlProperties". You
would have to use Reflection to set the value of that property, however.
I hope that helps.
--
John Saunders
johnwsaundersiii at hotmail
NT> "John Saunders" wrote:
>> > "Donald Welker" <Donald.WelkerN0$SPAM@N0$SPAM.navy.mil> wrote in message
> > news:23648D86-187A-4A9F-817A-B28F66642CFE@microsoft.com...> > > The log object in this case is a set of shim routines to write to the"wizard"> > event log, so it's not much of a risk at the moment. I'm writing aa> > to help build a complex document and have broken up sections into aboutfor> > dozen web controls, each with it's own edit and display panel. When the
> > user is done the final document is recorded in a database and presentedportion> > printing, so each control is supposed to be smart enough to edit itsdatabase> > of the draft document and display its fields in the final version. My
> > original thought was to write one module to communicate with thebut I> > and share it throughout, but it occurs to me that the controls will have
> > different data adapters and don't need access to each others' data --derive> > need to make sure the connection string is shared.> > >
> > > What I've now done is created a "template" control from which toI no> > the others, but now the derived controls won't load in the designer:
> > "Abstract Class" -- do I need to add a reference to the template or canthe> > longer use the designer?
> >
> > As to the connection and/or connection string, you could pass that intowhichever> > controls. Just add a Connection property of type SqlConnection (orwhat> > connection type you need).
> >
> > As to the "no inherited user controls" problem - that's just another
> > trade-off with user controls. The designer isn't clever enough to doproblem,> > you want there. If you used custom controls, you wouldn't have thisuser> > but you _would_ take longer to get the project done. You can have your>> > controls all inherit from a base class, but that base class can't do
> > anything as far as UI goes. I've done this successfully in the past.
John Saunders Guest



Reply With Quote

