.ASCX files, ASPX files and code behind

G

Guest

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.
 
M

Marina

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.
 
Y

Yup

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
 
M

Michel de Becdelièvre

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top