473,395 Members | 1,442 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,395 software developers and data experts.

Global Objects in User Controls

[note: I am relatively new to ASP.NET, so I'm very possibly doing something
stupid or overlooking something obvious. Please be forgiving, and walk me
through your answer as much as possible. Much appreciated.]

Here is the situation. I'm developing an application where:

I have defined a class (DBClass) to provide an abstraction layer for
interacting with a database. When an instance of the class is created, it
creates a connection to the database. My initial plan was that, at the very
top of each page I would:
1) include the DBClass class file, so the page knows about the class
2) create an instance of DBClass, which I call DB.
My thought was, once the DB instance was created, I would be able to use it
anywhere on the page.

However, on the page I am also making use of a "user control". This user
control has to have access to the database. When I simply reference DB in
the control file, the compiler complains, because I have not created the
instance DB in the control file.

I'm sure I'm probably just approaching this the wrong way or leaving out
something obvious.... can anyone help?

--G
Oct 30 '06 #1
1 1067
You could create a property on the user control that can then be set in the
page. So in the code in the page you would have userControl.ReferenceDb =
DB;

You'll have to toy with where you can make use of this. For example, the
Page_Load events of the user control and the page are fired at different
times so typically if you want to use a property in a control that was set
by the page you would write the code in an overloaded onprerender event.

--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"Greg Stevens" <Gr***********@gmail.comwrote in message
news:2A**********************************@microsof t.com...
[note: I am relatively new to ASP.NET, so I'm very possibly doing
something
stupid or overlooking something obvious. Please be forgiving, and walk me
through your answer as much as possible. Much appreciated.]

Here is the situation. I'm developing an application where:

I have defined a class (DBClass) to provide an abstraction layer for
interacting with a database. When an instance of the class is created, it
creates a connection to the database. My initial plan was that, at the
very
top of each page I would:
1) include the DBClass class file, so the page knows about the class
2) create an instance of DBClass, which I call DB.
My thought was, once the DB instance was created, I would be able to use
it
anywhere on the page.

However, on the page I am also making use of a "user control". This user
control has to have access to the database. When I simply reference DB in
the control file, the compiler complains, because I have not created the
instance DB in the control file.

I'm sure I'm probably just approaching this the wrong way or leaving out
something obvious.... can anyone help?

--G

Oct 30 '06 #2

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

Similar topics

1
by: Oystein Haare | last post by:
I'm thinking about sort of a factory-system where the factories get instanciated on program start (through a global object in the .cpp file), and registers themselves with a singelton. Is it a...
2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
3
by: Richard Payne | last post by:
Hi I am after what is the best/simplest way to pass objects between Web User Controls which doesn't use the session object. Regards Richard
0
by: Simon Harris | last post by:
Hi All, In the scenario of an Intranet site with some 50 applications, I am trying to work out the best way to achieve the following: - Global shared classes, which can be used in any...
2
by: Jon Hyland | last post by:
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...
3
by: Mr Newbie | last post by:
I am messing around with Web User Controls at present and (think) I have discovered the following. 1.) The identifier for the control in the code behind must match the ID for the control on the...
0
by: ben | last post by:
I am having some difficulty in understanding how to deal with variables, session states, objects, user controls while attempting to develop a ASP.Net application. I have created several user...
2
by: Amil Hanish | last post by:
I want to create a bunch of like user controls and display them in a DataList. Does anyone have a GOOD example of how to do this? I thought I had it...I added the user control to my...
8
by: | last post by:
I'm looking for some design guidance on a collection of projects I'm working on. The project involves a bunch of websites constructed out of a collection of user controls. Different user...
11
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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
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...

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.