PC Review


Reply
Thread Tools Rate Thread

Access user control from other user control

 
 
Chris Zopers
Guest
Posts: n/a
 
      20th Feb 2008
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("FirstUserControl")

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 ***
 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      21st Feb 2008
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" <(E-Mail Removed)> wrote in message
news:OXK3I$(E-Mail Removed)...
> 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("FirstUserControl")
>
> 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 ***



 
Reply With Quote
 
David Bunge
Guest
Posts: n/a
 
      5th Aug 2008
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 ***
 
Reply With Quote
 
Nanda Lella[MSFT]
Guest
Posts: n/a
 
      27th Apr 2009
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you access a control property of a main form from a custom user control? forest demon Microsoft C# .NET 6 22nd Aug 2007 11:36 PM
Populate data in one user control based on selection in another user control rajarameshvarma@gmail.com Microsoft C# .NET 1 28th Aug 2006 10:25 AM
Custom User Control is not rendered inside another user control in a server table. Leeor Chernov Microsoft ASP .NET 2 16th Oct 2005 09:35 AM
user control cant access ViewState of Parent control Steve Richter Microsoft ASP .NET 5 5th Apr 2005 05:14 PM
Re: Access Values in User Control from container control Karl Microsoft ASP .NET 0 15th Sep 2004 04:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:40 PM.