Master Page in ASP.Net


Well-Known Member
MasterPage in ASP.Net

Here in this section we will discuss about master page and its application in Master Page is a page which allow us to create a unique design or layout in the application website. A master page gives a good theme or look about the application website and can be used in all the pages or group of pages. The most advantage of using a master page is that it eradicates the problem of writing a same code in all pages.

Here I have explained with an example.

Creating a Master Page:

Step 1: Go to Website

Step 2: Click Add New Item

Step 3: Select Master Page

Step 4: Name the Master Page-> example-> secondmasterpage.master

Step 5: Clik Add

We will see a contentplaceholder appears.We now can design the page accordingly.keeping the contentplaceholder unchaged.ContentPlaceHolder is where the content of the other pages will appear.We can place the contentplaceholder where its needed.

The background colour is changed, there are many buttons added, and also a datalist which is bounded with all the categories name.

Now to add master page to other pages:

Step 1: Go to the desired website where the master page will be merged.example -> userviewone.aspx

Step 2: Go to the source part where the html tags are written.

Step 3: Here we can see the html codes. Now Excluding the <div>…..</div> delete all the tags from <!DOCTYPE to </html> including </body>, </form>.<head>.

Step 4: Now coming to the top most code.

The above code is changed to

Here we can see that after “C#” this code is added MasterPageFile="~/secondmasterpage.master"

Here Secondmasterpage.master is name of the masterpage that we have created.

Step 5: Also we can see

<asp:Content ID="conten1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
</asp:Content> Is also added.

Now running the page the page will appear like it should be.