views:

512

answers:

5

I have a global header and footer that has a Database connection and a disconnection in the header and footer and need to convert this to ASP.NET - this needs to be done before any HTML is output and after any HTML is output for the footer.
I cannot seem to find any easy way to do this, like in Classic ASP - if possible post an example or link to one instead of stating a given technique, thanks.


A bit more detail is that I have an SQL Connection string then a single SQL object I want to use throughout all the pages on the website - with the connection string and declarations in the header, and the disconnection in the footer.

+1  A: 

You can create the Header and Footer as a UserControl (either in code or with the visual designer) and add those controls to your page. YOu might want to be a little more clear in your question though....

Colin
Technically, this would work, but is not the best solutions. I suggest you look at using a MasterPage (as quoted below me), or abstracting your database connection/implementation into an appropriate layer. You really don't want to be adding data connection code like this in your view layer. Just makes things messy and hard to maintain.
James McConnell
+2  A: 

I am a little confused. But I think you might be looking for Master Pages in ASP.NET.

You make a master that controls the Header and Footer. Then add content placeholders to the places you want your other HTML content that changes page to page.

good tutorial is : http://webproject.scottgu.com/CSharp/MasterPages/MasterPages.aspx

BigBlondeViking
Thanks for the link to the master pages topic, may use this for visible headers and footers on website.
RoguePlanetoid
I try :) good luck
BigBlondeViking
+1  A: 

I think you could look at doing this via a master page - expose the connection to the content pages via a property.

Really, I'm not clear exactly what you mean from your question - you should probably take a look at asp.net page life cycle.

You can create your connection at the start of page processing and kill it when you've finished with it in the page - you can do all this without needing a master page.

Chris W
This may be handy for the look and feel of the pages as most have a consistent visible header, although this was not my question this may be useful anyway.
RoguePlanetoid
+3  A: 

You should not do it like in classic ASP, period. Your database connections should be dealt with at the appropriate layer - preferably you should have a data access and a business layer.

Otávio Décio
You are working against the model of the technology you are migrating to; please do a little more reading about how ASP.Net's page lifecycle works, it is **NOT** similar to the ASP model. You can write ASP3-like code in .Net (you can write Fortran in any language, after all), but in my experience it causes performance suffering and a lack of scalability.
Tetsujin no Oni
I guessed this, while doing it - if something is difficult - it is either very hard to understand, or is the wrong way - I realise now the latter was the case!
RoguePlanetoid
+3  A: 

Your use of the terms Header and Footer leads many to believe you actually intend to create HTML that forms a visible header and footer in the output. Hence the number of references to Master pages in the answers.

However I'm going to guess in a different direction. You have a classic ASP page which includes inline code from an include file, then sequentially the body in the main ASP page uses stuff created in the include, then there is final include file at the bottom which tidies up stuff created in the first include.

In ASP.NET you can't treat the content of the page as if it were a purely sequential script as you could in ASP. If you really must you could confgure stuff in the Page_Load event then tidy up in Page_Unload.

Do you really want to port the app without re-engineering it?. I can see little to gain by trying to copy the infrastructure of an old ASP app in ASP.NET. The goal surely is to deliver the same UI and functions (or better) to the client using ASP.NET in the way ASP.NET was intended to be used. This gives you a platform to move forward rather than create a strange looking ASP.NET app which starts life already a legacy piece of code.

AnthonyWJones
I agree refactoring in ASP.NET on a functional basis rather than porting the code or at least the way it was seperated was the wrong way of doing it.
RoguePlanetoid