Global Objects in User Controls

Discussion in 'ASP .Net' started by =?Utf-8?B?R3JlZyBTdGV2ZW5z?=, Oct 30, 2006.

  1. [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
     
    =?Utf-8?B?R3JlZyBTdGV2ZW5z?=, Oct 30, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > [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
     
    Mark Fitzpatrick, Oct 30, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harris

    global user controls and classes

    Simon Harris, Jun 26, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    340
    Simon Harris
    Jun 26, 2004
  2. 7stud
    Replies:
    11
    Views:
    711
    Dennis Lee Bieber
    Mar 20, 2007
  3. Faizan Ahmed
    Replies:
    1
    Views:
    299
    John Saunders
    Jan 4, 2005
  4. Faizan Ahmed
    Replies:
    1
    Views:
    376
    John Saunders
    Jan 4, 2005
  5. Greg Stevens

    Global Objects inside User Controls?

    Greg Stevens, Oct 30, 2006, in forum: ASP General
    Replies:
    2
    Views:
    126
    Greg Stevens
    Oct 30, 2006
Loading...

Share This Page