473,785 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global property

I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks
Nov 18 '05 #1
15 2515
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.P age
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
....
End Sub

....
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyVal ue
Every user control has access to the Page, and by casting it to the specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@com cast.net> wrote in message
news:eH******** ********@TK2MSF TNGP12.phx.gbl. ..
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks

Nov 18 '05 #2
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if you try
and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of test to
be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyVal ue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew******** ********@TK2MSF TNGP14.phx.gbl. ..
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.P age
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyVal ue
Every user control has access to the Page, and by casting it to the specific type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@com cast.net> wrote in message
news:eH******** ********@TK2MSF TNGP12.phx.gbl. ..
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks


Nov 18 '05 #3
Yes,
Your problem statement didn't mention that.

If you need this from multiple page, the best solution is to make them
implement an interface and cast the page to said interface...if this is what
you need, lemme know.

Karl

"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:uD******** ******@TK2MSFTN GP14.phx.gbl...
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if you try and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of test to
be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyVal ue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew******** ********@TK2MSF TNGP14.phx.gbl. ..
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.P age
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyVal ue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@com cast.net> wrote in message
news:eH******** ********@TK2MSF TNGP12.phx.gbl. ..
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks



Nov 18 '05 #4
Sorry Greg,..just realized you aren't the one who asked the original
question ... :)

Karl
"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:uD******** ******@TK2MSFTN GP14.phx.gbl...
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if you try and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of test to
be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyVal ue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew******** ********@TK2MSF TNGP14.phx.gbl. ..
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.P age
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyVal ue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@com cast.net> wrote in message
news:eH******** ********@TK2MSF TNGP12.phx.gbl. ..
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks



Nov 18 '05 #5
Ah, but I would love to see how to do that (the interface design) if you get
a chance. I have an app that utilizes a few user controls across a couple
of webforms. They all need to share the same state. My current method is
extremely ugly. I've made all the user controls have a public property (say
MySettings). In the page load I initialize all the usercontrols MySettings
properties to match the Page's MySettings. Every time there is a change, I
resync all the usercontrols (by raising custom events registered in the
hosting Page). It is very ugly as you can imagine.

I probably should have just shoved MySettings into a Session variable, but I
was trying to keep everything in ViewState instead. (Not sure I am made the
right decision here)

It is very interesting to me to be able to read a common property from the
hosting page directly. My problem is, like I mentioned, my usercontrols are
spread across mutliple webforms and doing a Select Case on a TypeOf seems
uglier yet.

Greg
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:e4******** *******@tk2msft ngp13.phx.gbl.. .
Sorry Greg,..just realized you aren't the one who asked the original
question ... :)

Karl
"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:uD******** ******@TK2MSFTN GP14.phx.gbl...
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if you

try
and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of test to be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyVal ue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:ew******** ********@TK2MSF TNGP14.phx.gbl. ..
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.P age
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyVal ue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@com cast.net> wrote in message
news:eH******** ********@TK2MSF TNGP12.phx.gbl. ..
> I have main aspx page with a number of user controls.
> How can I create a global property that will be visible in every user > control?
>
> Thanks
>
>



Nov 18 '05 #6
You can either do it with a base page or an interface.

Public Interface ISettings
ReadOnly Property Settings() As Hashtable
End Interface

your pages:

Public Class TestPage
Inherits Page
Implements ISettings

private _settings as Hashtable
Public ReadOnly Property Settings() As Hashtable Implements
ISettings.Setti ngs
Get
return _settings
End Get
End Property
...
End Class

Public Class TestPage2
Inherits Page
Implements ISettings

private _settings as Hashtable
Public ReadOnly Property Settings() As Hashtable Implements
ISettings.Setti ngs
Get
return _settings
End Get
End Property
...
End Class

your user control:

if typeof (Page) is ISettings then
settings = ctype(Page, ISettings).Sett ings
end if
Is that what you wanted?

Karl

"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:eZ******** ******@TK2MSFTN GP09.phx.gbl...
Ah, but I would love to see how to do that (the interface design) if you get a chance. I have an app that utilizes a few user controls across a couple
of webforms. They all need to share the same state. My current method is
extremely ugly. I've made all the user controls have a public property (say MySettings). In the page load I initialize all the usercontrols MySettings properties to match the Page's MySettings. Every time there is a change, I resync all the usercontrols (by raising custom events registered in the
hosting Page). It is very ugly as you can imagine.

I probably should have just shoved MySettings into a Session variable, but I was trying to keep everything in ViewState instead. (Not sure I am made the right decision here)

It is very interesting to me to be able to read a common property from the
hosting page directly. My problem is, like I mentioned, my usercontrols are spread across mutliple webforms and doing a Select Case on a TypeOf seems
uglier yet.

Greg
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:e4******** *******@tk2msft ngp13.phx.gbl.. .
Sorry Greg,..just realized you aren't the one who asked the original
question ... :)

Karl
"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:uD******** ******@TK2MSFTN GP14.phx.gbl...
Karl,

This is quite interesting. But something doesn't seem right to me. A
usercontrol can be placed on any page. Won't this cause problems if
you
try
and cast the Page object it is "hosted" in to a specific class?

Problems may not be the right word. Shouldn't you do some sort of
test to be sure Page is of the correct type before casting?

If TypeOf (Page) Is WebForm1 Then
dim pageValue as string = ctype(Page, WebForm1).MyVal ue
End If

Greg

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:ew******** ********@TK2MSF TNGP14.phx.gbl. ..
> create a public property
>
> Say your page is of type WebForm1
>
> Public Class WebForm1
> inherits System.Web.UI.P age
>
>
> private _myvalue as string = "SomeValue"
> public readonly property MyValue() as string
> get
> return _myvalue
> end get
> end property
>
> Sub Page Load...
> ...
> End Sub
>
> ...
> End class
>
> In your user controls, you can access MyValue via:
>
> dim pageValue as string = ctype(Page, WebForm1).MyVal ue
>
>
> Every user control has access to the Page, and by casting it to the
specific
> type, you have access to it's public property/methods..
>
> Karl
>
>
> "Mark Goldin" <ma********@com cast.net> wrote in message
> news:eH******** ********@TK2MSF TNGP12.phx.gbl. ..
> > I have main aspx page with a number of user controls.
> > How can I create a global property that will be visible in every user > > control?
> >
> > Thanks
> >
> >
>
>



Nov 18 '05 #7
I believe so. Thanks!

Greg

Is that what you wanted?

Karl

Nov 18 '05 #8
How do I reference such a property from a user control?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew******** ******@TK2MSFTN GP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.P age
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyVal ue
Every user control has access to the Page, and by casting it to the specific type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@com cast.net> wrote in message
news:eH******** ********@TK2MSF TNGP12.phx.gbl. ..
I have main aspx page with a number of user controls.
How can I create a global property that will be visible in every user
control?

Thanks


Nov 18 '05 #9
In your usercontrol, you just do like Karl says:

dim pageValue as string = ctype(Page, WebForm1).MyVal ue

where MyValue is a property of your WebForm1 class

Now, if you go the extra mile and make your WebForm1 implement an interface
(says ISettings), then you can do this:

dim pageValue as string = ctype(Page, ISettings).MyVa lue

Which is more generic and useful.

Let me know if you need more help. Not sure about the language barrier.
(I'm a VB'er)
Greg
"Mark Goldin" <ma********@com cast.net> wrote in message
news:OT******** ******@TK2MSFTN GP11.phx.gbl...
How do I reference such a property from a user control?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ew******** ******@TK2MSFTN GP14.phx.gbl...
create a public property

Say your page is of type WebForm1

Public Class WebForm1
inherits System.Web.UI.P age
private _myvalue as string = "SomeValue"
public readonly property MyValue() as string
get
return _myvalue
end get
end property

Sub Page Load...
...
End Sub

...
End class

In your user controls, you can access MyValue via:

dim pageValue as string = ctype(Page, WebForm1).MyVal ue
Every user control has access to the Page, and by casting it to the

specific
type, you have access to it's public property/methods..

Karl
"Mark Goldin" <ma********@com cast.net> wrote in message
news:eH******** ********@TK2MSF TNGP12.phx.gbl. ..
> I have main aspx page with a number of user controls.
> How can I create a global property that will be visible in every user
> control?
>
> Thanks
>
>



Nov 18 '05 #10

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

Similar topics

2
1952
by: Aaron | last post by:
Hi, I've seen javascript code where a constructor function is passed an argument "document", and inside the function itself the assignment "this.document = document;" is made. This is the code (or the part necessary for the example): function ToggleButton(document) { ToggleButton.images = new Array(4); for(i=0;i<4;i++) { ToggleButton.images = new
8
2559
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
3
1724
by: Steve | last post by:
Visual Studio 2003 / C# My application has 1 main form. On this form is a treeview object down the left hand edge and a status bar along the bottom. That is all. The tree view acts as my menu controller, and when the users click an item, I generate instances of various UserControls I have for each of the menu items, and add an instance to the Main form in the right hand area. Some of these UserControls are just single page objects...
33
3051
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
16
3234
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for me. I was doing research on JavaScript inheritance recently (simplifying it in particular) and after reading 10.1.1, 10.1.3 and some other sections of ECMA262 I got a hint on accessing global object from different than global scope.
23
2265
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over confused things, so I will start afresh. An example of what I want to do...
3
2774
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions doesn't actually give any insight.
3
942
by: rusdyrip | last post by:
hi all, anyone to know how to make Global Property so if i change Font size, all form on my application will change did i must change the designer every form? thx
15
2054
by: Bob | last post by:
Is there anyway to access the global object from inside a function other than doing a "var _global = this;" before declaring the function? Thanks
0
9480
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
10147
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...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
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...
0
5379
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.