Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Access user control from other user control (http://www.velocityreviews.com/forums/t592970-access-user-control-from-other-user-control.html)

Chris Zopers 02-20-2008 02:49 PM

Access user control from other user control
 
Hello,

I have two UserControls on a aspx page. I need to read a property of the
first UserControl from within the second UserControl. The UserControls
only know about themselves, so I can't declare a variable of the other
usercontrol's type. So I can't do something like this in the second
UserControl:

FirstUserControl first =
(FirstUserControl)this.Parent.FindControl("FirstUs erControl")

Additionally I don't like to use the FindControl method, because it
returns an Object.

What's the best way to get to a usercontrol's properties from within an
other user control?

Greetings,
Chris




*** Sent via Developersdex http://www.developersdex.com ***

Eliyahu Goldin 02-21-2008 10:29 AM

Re: Access user control from other user control
 
In the first user control, you can declare a property of the type matching
the second user control and set this property from the page containing both
controls.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"Chris Zopers" <test123test12@12move.nl> wrote in message
news:OXK3I$8cIHA.1208@TK2MSFTNGP03.phx.gbl...
> Hello,
>
> I have two UserControls on a aspx page. I need to read a property of the
> first UserControl from within the second UserControl. The UserControls
> only know about themselves, so I can't declare a variable of the other
> usercontrol's type. So I can't do something like this in the second
> UserControl:
>
> FirstUserControl first =
> (FirstUserControl)this.Parent.FindControl("FirstUs erControl")
>
> Additionally I don't like to use the FindControl method, because it
> returns an Object.
>
> What's the best way to get to a usercontrol's properties from within an
> other user control?
>
> Greetings,
> Chris
>
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***




David Bunge 08-05-2008 03:34 PM

Re: Access user control from other user control
 
How would you go about doing this? Could you post an example?

I have one user control declared as "public partial class
controls_SecondaryDisabilities : System.Web.UI.UserControl" on a page
with a second user control declared as "
public partial class controls_StateList : System.Web.UI.UserControl".

I need to get a value from the second user control (the StateList) into
a variable in the first (the SecondaryDisabilities). The value is a
public string, so it is accessible. However, when I declare "protected
controls_StateList ddlPartState;", the controls_StateList is not being
found.

Thank you in advance.

*** Sent via Developersdex http://www.developersdex.com ***

Nanda Lella[MSFT] 04-27-2009 10:32 PM

Re: Access user control from other user control
 
I assume both the UserControls are in the same page. If So, You can use
public properties in both the UserControls and set them when needed.
I have poseted a similar article with example code here
http://ctrlf5.net/?p=53

--

Thank You,
Nanda Lella,
http://www.CtrlF5.net
This Posting is provided "AS IS" with no warranties, and confers no rights.



All times are GMT. The time now is 09:35 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.