473,624 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I Share/Access properties between User Controls??

This might be a dumb question, but what is the best way for one
instance of a user control to access properties of an instance of
another user control?

For example, let's say I have an instance of UserControlA (ucA1) and
an instance of UserControlB (ucB1) on a particular ASP.NET page.
UserControlA has a string property called StringA that gets generated
from a DB call in it's Page_Load event. I want UserControlB to be
able to access UserControlA's StringA property in its Page_Load event
(which gets fired afterward). How would you do this?

Would you need to create a page level variable and assign the value to
this? Is it even possible for a user control to access its
container's objects?

I want to avoid using Session variables here.. the text contained in
this string should not persist in memory beyond the page call.

Thanks in advance!
Nov 18 '05 #1
2 2346

You could use a page level variable as you stated.

Here's one way I did it:

Say the page holding these controls
is MainPage.ascx

Create a property in MainPage like this:

Public Property StringA() As String
Get
Return CType(viewstate ("StringA"), String)
End Get
Set(ByVal Value As String)
viewstate("Stri ngA") = Value
End Set
End Property
Then in each control you could have

Protected MyParent as MainPage

and refer to MyParent.String A

(Actually I created a base class that each user control inherits, and
put MyParent there)

HTH,
Jim
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #2

"Jon Hyland" <jo*******@hotm ail.com> wrote in message
news:f2******** *************** ***@posting.goo gle.com...
This might be a dumb question, but what is the best way for one
instance of a user control to access properties of an instance of
another user control?

For example, let's say I have an instance of UserControlA (ucA1) and
an instance of UserControlB (ucB1) on a particular ASP.NET page.
UserControlA has a string property called StringA that gets generated
from a DB call in it's Page_Load event. I want UserControlB to be
able to access UserControlA's StringA property in its Page_Load event
(which gets fired afterward). How would you do this?

Would you need to create a page level variable and assign the value to
this? Is it even possible for a user control to access its
container's objects?

I want to avoid using Session variables here.. the text contained in
this string should not persist in memory beyond the page call.

Thanks in advance!


FYI -

What I ended up doing was creating a class with the globals I wanted to
share between user controls, and put a public instance of this class on my
main page.

Then in the user controls I do something like this:

//to set the global value in one user control
((ProjectNamesp ace.MainPage)th is.Page).MyClas s.sStringA = "1234";

//to retrieve from another user control
string sValue = ((ProjectNamesp ace.MainPage)th is.Page).MyClas s.sStringA;

Each user control has a "Page" property that points to it's container. You
have to cast this property to the specific page it refers to, then you can
access its public members.

Of course, this makes the user control useful only on this specific page
(you can't put it on a page other than MainPage), but this is ok for what I
need it for.. I'm not reusing these controls.

If I end up wanting to reuse these controls there is another method of
calling contriner functions using delegates.
http://www.serversidecode.net/absolu...id=97&zoneid=8 shows some good examples of this.
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3352
by: deko | last post by:
I'm hoping someone can sanity check my understanding of the Object Model for Forms/Controls. I'm having trouble drilling down into Control properties. First, I have a record set with the following for a given Form: 1)Container
6
4738
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
16
7211
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
5
1952
by: jqpdev | last post by:
Hello all... I'm coming from a Borland Delphi background. Delphi has a specific component called a Data Module. In the designer the Data Module behaves like a windows form. A developer can drop non-visual (controls) on the data module surface and wire them up and create procedures, functions, event procedures. In the source file (code behind file) the Data Module is a class, and the dropped components are public properties. The...
5
3042
by: djscratchnsniffing | last post by:
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?
4
1446
by: dx | last post by:
I have 3 vb.net web applications. I would like all 3 to have access to a library of user controls. server controls wouldn't cause a problem but these are user controls (ascx.) From each solution I reference the user control library project and create a virtual directory under each applicatoin that links to the folder that contains the ascx files but I get a runtime error like: Description: An error occurred during the parsing of a...
2
5480
by: yxq | last post by:
Hello I want to create and delete the folder share, i found that it is ok for generic folder, but it does not work for Root directory(i.e c:\, d:\) The code **************************************************** Dim mc As New ManagementClass("Win32_Share") Dim mo As ManagementObject Dim inParams As ManagementBaseObject Dim outParams As ManagementBaseObject
2
14970
by: mail | last post by:
Hi guys, So far I've spent about a week hacking away at this code, and I just can't get it to add an ACE to a the DACL for a network share using WMI. Just to set the scene, I'm trying to add an ACL from machine A (workstation; Saturn) and set it to a UNC path (\\Mercury\Inetpub\Websites\LocalUser\Test) on machine B (server; Mercury).
7
2289
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using FindControl the only way to do it?
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.