473,387 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2331

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("StringA") = Value
End Set
End Property
Then in each control you could have

Protected MyParent as MainPage

and refer to MyParent.StringA

(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*******@hotmail.com> wrote in message
news:f2**************************@posting.google.c om...
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
((ProjectNamespace.MainPage)this.Page).MyClass.sSt ringA = "1234";

//to retrieve from another user control
string sValue = ((ProjectNamespace.MainPage)this.Page).MyClass.sSt ringA;

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
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...
6
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...
16
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
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...
5
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...
4
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...
2
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...
2
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.