Understanding Master Page

Master Page - maintains a consistent looking behaviours.ContentPageHolder - Region where Content pages can be plugged in. Master Pages are merged into content pages.
MasterPageFile="~/Site1.Master"  -->Content pages have tis directive inside the design page ,which indicate that it is associated with MasterPageFile
ContentPlaceHolderID-> established link between masterpage and content page holder

Passing data from content page to master page in asp net

1)With the help of public property of master page and exposing the property to content pages we can enable data
transmission from content page to masterpage

2)<%@ MasterType VirtualPath="~/Site1.master" %> this directive to the contenet page that will enebale us to get the property from master page to the contenet page without any type casting

Page.Master property always returns an object of type MasterPage and we need to explicitly typecast it to the actuall master page, if we need to access its property and methods

Where as Master Property returnd a striongly types reference of the actual amasterpage if the contenet page has Master type directive specified, Otherwise Master property works in the same way as Page.master property


Turn Off The visibility of control on MasterPage to a Content Page
We can create a public property of a panel and place a controls inside the panel and access the public property from content page and make the visibility as false

Master Page :


<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="understandingMasterPage.Site1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2" style="width: 800px; height: 80px; background-color: #b6ff00; text-align: center">
                    <h1>WebsiteHeader
                    </h1>
                    <asp:Panel ID="panelSearch" runat="server">
                    <b>Search:</b>
                    <asp:TextBox ID="textBoxMaster" runat="server"></asp:TextBox>
                    <asp:Button ID="buttonMaster" runat="server" Text="Search" />
                    </asp:Panel>
                </td>
            </tr>
            <tr>
                <td style="height: 500px; background-color: #808080; width: 150px">
                    <h3>Menu
                    </h3>
                    <br />
                    <asp:TextBox ID="TextBox1MasterPage" runat="server"></asp:TextBox>


                </td>
                <td style="height: 500px; background-color: #0094ff; width: 650px">

                    <asp:ContentPlaceHolder ID="MainContentPlaceHolder" runat="server">
                        <h2>This section changed on page to page basis
                        </h2>
                    </asp:ContentPlaceHolder>
                   
                </td>
            </tr>
            <tr>
                <td colspan="2" style="background-color: #b6ff00; text-align: center">
                    <b>Website Footer</b>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>


MasterPage.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace understandingMasterPage
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public TextBox TextBoxOnMasterPage
        {
            get
            {
                return this.TextBox1MasterPage;
            }

        }

        public Panel PanelSearch
        {
            get
            {
                return this.panelSearch;
            }
        }

        public Button ButtonSearch
        {
            get
            {
                return this.buttonMaster;
            }
        }

        public string SearchTerm
        {
            get
            {
                return this.textBoxMaster.Text;
            }
        }

    }
}

Content Page 1:
 --------------------- --------------------- --------------------- --------------------- ---------------------

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="understandingMasterPage.WebForm1" %>
<%@ MasterType VirtualPath="~/Site1.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <h1> This is webform1 Content </h1>
    <b> Please Enter Some Text</b>
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Set Button" OnClick="Button1_Click" />
</asp:Content>


Content Page1.CS
 --------------------- --------------------- --------------------- --------------------- ---------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace understandingMasterPage
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //((Site1)Master).TextBoxOnMasterPage.Text = TextBox1.Text;  //Adding Cast
            Master.TextBoxOnMasterPage.Text = TextBox1.Text; //Strongly typed reference
        }
    }
}

Content Page 2 .aspx
-------------------------------------------------------------------------------------------------
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="understandingMasterPage.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <h1> This is Webform 2</h1>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
</asp:Content>

ContentPage 2 .cs
---------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace understandingMasterPage
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Content Page 3 .aspx
 ------------- ------------- ------------- ------------- ------------- ------------- -------------

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="understandingMasterPage.WebForm3" %>
<%@ MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</asp:Content>

Content Page 3 .cs
 ------------- ------------- ------------- ------------- ------------- ------------- -------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace understandingMasterPage
{
    public partial class WebForm3 : System.Web.UI.Page
    {

        protected void Page_Init(object sender, EventArgs e)
        {
            Master.ButtonSearch.Click+=ButtonSearch_Click;
        }

        private void ButtonSearch_Click(object sender, EventArgs e)
        {
            getData(Master.SearchTerm);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                getData(null);

        }

        private void getData(string searchTeam)
        {
            string cs = ConfigurationManager.ConnectionStrings["DBCONN"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("getEmployees", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter serachParam = new SqlParameter("@name", searchTeam ?? string.Empty);
                cmd.Parameters.Add(serachParam);
                conn.Open();

                GridView1.DataSource = cmd.ExecuteReader();
                GridView1.DataBind();

            }
        }
    }
}

WebConfig
 ------------- ------------- ------------- ------------- ------------- ------------- -------------

<connectionStrings>
  <add name="DBCONN" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=H:\DatabaseTest\DatabaseTest\DatabaseTest.mdf;Integrated Security=True;Connect Timeout=30" 
       providerName="System.Data.SqlClient" />
</connectionStrings>

SQL File
-------------- ------------- ------------- ------------- ------------- ------------- -------------
 CREATE procedure getEmployees
 @name varchar(20)
as
BEGIN
SELECT * from Employee  where Name like '%'+@name+'%'
END

Content Page4.aspx
 ------------- ------------- ------------- ------------- ------------- ------------- -------------

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="understandingMasterPage.WebForm4" %>
<%@ MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
</asp:Content>

Content Page5.aspx
 ------------- ------------- ------------- ------------- ------------- ------------- -------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace understandingMasterPage
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Master.PanelSearch.Visible = false;

        }
    }
}


Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

AngularJs Tutorial-1

Can a derived class reference contain base class object.