Hi imoleayoemman,
You need to make use of page_PreInit event handler.
Set the required master page in Session and using MasterPageFile property retrieve the master page from Session and set it.
Refer below example.
Site1.master
<html>
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body style="background-color: azure">
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Site2.master
<html>
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body style="background-color: antiquewhite">
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<asp:Button Text="Change" runat="server" OnClick="OnChange" />
</form>
</body>
</html>
Default
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<h3>Default</h3>
</asp:Content>
AdminHome
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<h3>Admin Home</h3>
</asp:Content>
Code
Site2.master.cs
protected void OnChange(object sender, EventArgs e)
{
Session["MasterPage"] = "Site2.Master";
Response.Redirect("Default.aspx");
}
Default.aspx.cs
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
page.MasterPageFile = this.GetMasterPageFromSession();
}
protected string GetMasterPageFromSession()
{
if (Session["MasterPage"] == null)
{
return "~/Site1.Master";
}
else
{
return Session["MasterPage"].ToString();
}
}
Screenshot
