473,795 Members | 2,967 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
15 2516
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:OV******** ******@tk2msftn gp13.phx.gbl...
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 #11
That makes two of us. :^)

Greg

"Mark Goldin" <ma********@com cast.net> wrote in message
news:OZ******** ********@TK2MSF TNGP14.phx.gbl. ..
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:OV******** ******@tk2msftn gp13.phx.gbl...
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 #12

string pageValue = ((WebForm1)Page ).MyValue

or via the interface:

string pageValue ((ISettings)Pag e).MyValue

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Mark Goldin" <ma********@com cast.net> wrote in message
news:OZ******** ********@TK2MSF TNGP14.phx.gbl. ..
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:OV******** ******@tk2msftn gp13.phx.gbl...
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 #13
After all here is what I have:
in Webform (main2):
private DataSet _Employee = null;

public DataSet Employee

{

get

{return _Employee;}

set

{_Employee = value;}

}

in the user control:

DataSet ds = ((humanres.main 2)Page).Employe e;

....

SqlHelper.FillD ataset(conn, CommandType.Tex t, "select * from udf_getEmployee Details(" + EmployeeId + ")",

ds, new string[]{"employee"} );

fails with the following error:



Server Error in '/humanres' Application.
--------------------------------------------------------------------------------

Value cannot be null. Parameter name: dataSet
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Argument NullException: Value cannot be null. Parameter name: dataSet
Line 53: DataSet ds = ((humanres.main 2)Page).Employe e;
.....

Line 55: SqlHelper.FillD ataset(conn, CommandType.Tex t, "select * from udf_getEmployee Details(" + EmployeeId + ")",
Line 56: ds, new string[]{"employee"} );


"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:eV******** ******@TK2MSFTN GP11.phx.gbl...

string pageValue = ((WebForm1)Page ).MyValue

or via the interface:

string pageValue ((ISettings)Pag e).MyValue

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Mark Goldin" <ma********@com cast.net> wrote in message
news:OZ******** ********@TK2MSF TNGP14.phx.gbl. ..
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:OV******** ******@tk2msftn gp13.phx.gbl...
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 #14
Mark,
Your dataset is null...you never do _Employee = new DataSet()...The simplest
way to solve your problem:

private DataSet _Employee = new DataSet(); //instead of = null;

The reason no on suggested this is that declaring a dataset in a page and
populating it in a user-control isn't obvious (at best) and suspect (at
worst). I would consider examining if that's a sound architecture. Since
I'm not sure what you are trying to do, short of your initial post asking
how to make a variable visible to all user controls, I can't offer any
suggestions.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Mark Goldin" <ma********@com cast.net> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
After all here is what I have:
in Webform (main2):
private DataSet _Employee = null;
public DataSet Employee
{
get
{return _Employee;}
set
{_Employee = value;}
}
in the user control:
DataSet ds = ((humanres.main 2)Page).Employe e;
....
SqlHelper.FillD ataset(conn, CommandType.Tex t, "select * from
udf_getEmployee Details(" + EmployeeId + ")",
ds, new string[]{"employee"} );
fails with the following error:
Server Error in '/humanres' Application.
Value cannot be null. Parameter name: dataSet
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Argument NullException: Value cannot be null.
Parameter name: dataSet

Line 53: DataSet ds = ((humanres.main 2)Page).Employe e;
.....
Line 55: SqlHelper.FillD ataset(conn, CommandType.Tex t, "select * from
udf_getEmployee Details(" + EmployeeId + ")",
Line 56: ds, new string[]{"employee"} );
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:eV******** ******@TK2MSFTN GP11.phx.gbl...

string pageValue = ((WebForm1)Page ).MyValue

or via the interface:

string pageValue ((ISettings)Pag e).MyValue

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Mark Goldin" <ma********@com cast.net> wrote in message
news:OZ******** ********@TK2MSF TNGP14.phx.gbl. ..
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:OV******** ******@tk2msftn gp13.phx.gbl...
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 #15
Your ds that your are passing to SqlHelper.Fill is still null. It has never been istantiated.

The VB equivalent is you have this: (not sure have to express this in c#)

Dim conn As New SqlClient.SqlCo nnection(Config urationSettings .AppSettings("C onnectionString "))
Dim ds As DataSet
SqlHelper.FillD ataset(conn, CommandType.Tex t, "select * from users where empid=1", ds, New String() {"employee"} )

You need to have this:
Dim conn As New SqlClient.SqlCo nnection(Config urationSettings .AppSettings("C onnectionString "))
Dim ds As NEW DataSet ' <---- gotta istantiate it first!!!
SqlHelper.FillD ataset(conn, CommandType.Tex t, "select * from users where empid=1", ds, New String() {"employee"} )
The reason your dataset is null, is becuase you assign it to the Employee ds property of your webform. And your not showing any code where you istantiate it anywhere.

These little bits and pieces of code without context are not helping me. (Maybe somebody else can help out here... please!)

Greg

"Mark Goldin" <ma********@com cast.net> wrote in message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
After all here is what I have:
in Webform (main2):
private DataSet _Employee = null;

public DataSet Employee

{

get

{return _Employee;}

set

{_Employee = value;}

}

in the user control:

DataSet ds = ((humanres.main 2)Page).Employe e;

...

SqlHelper.FillD ataset(conn, CommandType.Tex t, "select * from udf_getEmployee Details(" + EmployeeId + ")",

ds, new string[]{"employee"} );

fails with the following error:
Server Error in '/humanres' Application.
------------------------------------------------------------------------------

Value cannot be null. Parameter name: dataSet
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Argument NullException: Value cannot be null. Parameter name: dataSet
Line 53: DataSet ds = ((humanres.main 2)Page).Employe e;
....

Line 55: SqlHelper.FillD ataset(conn, CommandType.Tex t, "select * from udf_getEmployee Details(" + EmployeeId + ")",
Line 56: ds, new string[]{"employee"} );


"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:eV******** ******@TK2MSFTN GP11.phx.gbl...

string pageValue = ((WebForm1)Page ).MyValue

or via the interface:

string pageValue ((ISettings)Pag e).MyValue

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Mark Goldin" <ma********@com cast.net> wrote in message
news:OZ******** ********@TK2MSF TNGP14.phx.gbl. ..
I have no idea how to do that in C#.

"Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
news:OV******** ******@tk2msftn gp13.phx.gbl...
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 #16

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
2560
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
3052
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
3235
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
2775
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
2057
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
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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
10436
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
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
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3
2920
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.