.ASCX files, ASPX files and code behind

Discussion in 'ASP .Net' started by =?Utf-8?B?UGF1bA==?=, Sep 26, 2005.

  1. I have a .aspx site.

    I have a header that I want on the entire site. So I made it a User Control
    ..ascx file. In this header, I make one SQL call, so I need to create all the
    connection objects, etc.

    So I embed that User Control in the .ascx file. No problem.

    The problem is I have to make another SQL call in the .aspx file. But it
    appears that I can not use the same connection object that I declared in the
    ..ascx file.

    I do not want to create multiple connection object for each page.

    Is there a proper way to do this considering the requirements? Maybe use a
    code-behind. I only want to create one connection object per page.
     
    =?Utf-8?B?UGF1bA==?=, Sep 26, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?UGF1bA==?=

    Marina Guest

    Why not? As long as you close each connection after you are done with it, it
    will be fine (unless we are talking many many connections).

    You can also do some things like have a base page with a connection property
    exposed, and then have all your user controls get the current page's
    connection via this property.

    "Paul" <> wrote in message
    news:D...
    >I have a .aspx site.
    >
    > I have a header that I want on the entire site. So I made it a User
    > Control
    > .ascx file. In this header, I make one SQL call, so I need to create all
    > the
    > connection objects, etc.
    >
    > So I embed that User Control in the .ascx file. No problem.
    >
    > The problem is I have to make another SQL call in the .aspx file. But it
    > appears that I can not use the same connection object that I declared in
    > the
    > .ascx file.
    >
    > I do not want to create multiple connection object for each page.
    >
    > Is there a proper way to do this considering the requirements? Maybe use a
    > code-behind. I only want to create one connection object per page.
     
    Marina, Sep 26, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?UGF1bA==?=

    Yup Guest

    Behind the scenes, ADO.NET manages a connection pool for you (by default).
    It will re-use connections out of this pool or create new ones for you in a
    fashion somewhat independent of you creating connection objects in your
    code.

    -HTH


    "Paul" <> wrote in message
    news:D...
    >I have a .aspx site.
    >
    > I have a header that I want on the entire site. So I made it a User
    > Control
    > .ascx file. In this header, I make one SQL call, so I need to create all
    > the
    > connection objects, etc.
    >
    > So I embed that User Control in the .ascx file. No problem.
    >
    > The problem is I have to make another SQL call in the .aspx file. But it
    > appears that I can not use the same connection object that I declared in
    > the
    > .ascx file.
    >
    > I do not want to create multiple connection object for each page.
    >
    > Is there a proper way to do this considering the requirements? Maybe use a
    > code-behind. I only want to create one connection object per page.
     
    Yup, Sep 27, 2005
    #3
  4. "Paul" <> a écrit dans le message de news:
    ...

    > The problem is I have to make another SQL call in the .aspx file. But it
    > appears that I can not use the same connection object that I declared in
    > the
    > .ascx file.
    >
    > I do not want to create multiple connection object for each page.
    >
    > Is there a proper way to do this considering the requirements? Maybe use a
    > code-behind. I only want to create one connection object per page.


    The Page object that you get in your ascx is of the aspx's class. You
    can expose public methods on it (or declare the base page to derive from an
    interface if you need to be generic) and call them. Note however that the
    intialisation order is somewhat random, you need to code with this in mind .

    However, I would not do that unless I have a significant amount of data
    to share (a dataset for instance).
     
    Michel de Becdelièvre, Sep 27, 2005
    #4
    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. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    643
    Alphonse Giambrone
    Jun 28, 2003
  2. Holger (David) Wagner
    Replies:
    2
    Views:
    1,979
    Holger (David) Wagner
    Jul 3, 2004
  3. Paolo Pignatelli
    Replies:
    6
    Views:
    2,232
    Paolo Pignatelli
    Feb 21, 2005
  4. Nathan Sokalski
    Replies:
    1
    Views:
    533
    Nathan Sokalski
    May 15, 2007
  5. Robin Chatterjee

    Reuse an existing user control (.ascx) and its code behind in othe

    Robin Chatterjee, Dec 24, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    207
    Robin Chatterjee
    Dec 24, 2004
Loading...

Share This Page