Master Pages in VB.Net 2.0
This month i wrote just one article because i was very busy to develop an ecommerce software. I just finished it and i learned new interesting things.
I had to Develop two ecommerce sites for my customer so i preferred to make a really versatile ecommerce solution that could be skinned with templates.
I thought that i had to make something that could cover these aspects:
- Fully skinnable ecommerce solution so i just need to make html for new customers commerce sites
- Fully customizable database connections
- Fully object oriented to make easier to change things
- Fully customizable email messages
- Fully customizable payment methods
- Fully customizable products structure so the customer could sell everything giving the maximum parameters details
- Unlimited category tree
In the next articles i’ll wrote i’ll cover all the single aspects of this development experience but now i’d like to talk about the more interesting thing i found in asp.net 2.0, the MasterPage architecture.
The main things to understand about MasterPages are these:
- A MasterPage is a special asp.net 2.0 page (a template page) that can be filled with the HTML Layout of the site so you’ll never need to copy HTML in each site’s page.
- In a Master Page you can define the content areas using the new ContentPlaceHolder control (it’s just a container where the site “pieces” will be placed).
- A Master Page acts as a base class for all the site pages you’ll create, so you can write common source code inside it. Ex.: You can check user authentication in the master page if you prefer
- A website can have all the MasterPages you want and you can load the master page at runtime too. It’s very useful if you plan to use multiple template sites.
Whe you add a new asp.net page to your project, visual studio asks you which master page to use for the new page, you just have to choose one. Consider to make a default template master page and assign it to each page of your site.
Create a simple Asp.net website project and try to add a new MasterPage, fill it with HTML, create ContentPlaceHolders and then add a new WebForm choosing the Master Page. In your new page you’ll see the HTML layout (not editable) and the content placeholders where you can place the page specific content.
A really important thing is the Master Page loading at runtime. I found this functionality really exciting because it solved all my problems.
How to load a master page at runtime
Loading a master page at runtime is really simple, you just have to create the PreInit function for your new pages:
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Me.MasterPageFile = Application(“Template”) + “.master”
End Sub
As you can see i have the template name into Application, Application(”Template”).
You can change the site template with just one row of code:
Application(”Template”)=”TemplateBlue”
or
Application(”Template”)=”MyNewTemplate”
remember that to have this functionality you’ll need to write two masterpages:
TemplateBlue.master and MyNewTemplate.master
In this article i’ll not write source code, it’s really simple to do this, just try and in 10 minutes you’ll finish it
TAGS: Asp.net, dotnet, code snippet, seo, search engine optimization, visual studio.net, sample code, c#, vb.net
























yogita ghotkar said
am July 20 2007 @ 7:22 am
Hello ,
It was nice guideline but as i am new to the .net tech …..
can you give some more guideline
thanks