Adding Master Page to an existing Page dynamically when a condition existis

Posted by Write2varun under ASP.NET on 9/13/2013 | Points: 10 | Views : 901 | Status : [Member] | Replies : 7
Hello,

I have created a web page, now i want to add the master page, the problem is i only have to show the master page when the user enters and if the administrator enters the master page should be disabled and it should show the page as it is



void Page_PreInit(Object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
if (Session["login"].ToString() != "admin")
{
this.MasterPageFile = "~/NewMaster.master";
}
}
}
catch (Exception ex)
{
lable1.text = ex.ToString();
}
}


i also tried
this.Page.MasterPageFile = "~/NewMaster.master";


exception:

"Content controls have to be top-level controls in a content page or a nested master page that references a master page"

when i try to put Content controls in the page it gave me:

"ContentPlaceHolder can only be used in .master files."


it is not working.
please help
thank you




Responses

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Sample
This is the content page, which allows users to select a master page based on a query string provided with the request. The @ MasterType directive, which assigns a strong type to the page's Master property, references the base type.
<%@ Page Language="C#" Title="Content Page" MasterPageFile="~/MasterBlue.master"%>

<%@ MasterType TypeName="BaseMaster" %>
<script runat="server">
protected void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "MasterBlue.master";
if(Request.QueryString["color"] == "green")
{
this.MasterPageFile = "MasterGreen.master";
}
this.Title = Master.MyTitle;
}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
Content from Content page.
</asp:Content>


Reference: http://msdn.microsoft.com/en-us/library/c8y19k6h(v=vs.90).aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
To resolve above error refer
http://stackoverflow.com/questions/835877/master-page-weirdness-content-controls-have-to-be-top-level-controls-in-a-con

Specifying the Master Page Programmatically
http://www.asp.net/web-forms/tutorials/master-pages/specifying-the-master-page-programmatically-cs

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Write2varun on: 9/13/2013 [Member] Starter | Points: 25

Up
0
Down
the problem is i have head body and scripts in the aspx page

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Finally you sorted out....
Post us back if you have any issues later

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Write2varun on: 9/13/2013 [Member] Starter | Points: 25

Up
0
Down
i know what is the problem i cannot move the head body and script in the master page i just want to access the master for the user and for admin the regular page should come

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this once...
http://stackoverflow.com/questions/9149588/how-can-i-hide-the-menu-of-master-page-until-the-user-do-login-in-asp-net
For Dynamic themes refer
http://www.codeproject.com/Articles/14505/Dynamic-Themes-in-ASP-NET-2-0-C

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Write2varun on: 9/13/2013 [Member] Starter | Points: 25

Up
0
Down
this is not my problem. my problem is that i created the aspx page without master page
then i created master page
now i want the master page to get enabled only for user and not for admin

Write2varun, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response