Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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";

it is not working.
please help
thank you
Posted
Updated 12-Sep-13 20:50pm
v3
Comments
Harshil_Raval 13-Sep-13 2:51am    
So, have you got any error in this code?
write2varun 13-Sep-13 2:54am    
im getting an HTTP exception
write2varun 13-Sep-13 2:55am    
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
Harshil_Raval 13-Sep-13 3:05am    
Check, your web page don't have any <head> or <body> tags, it should have only <asp:Content..> tag. If you have all html structure in your web page and if you add masterpage file to your web form, then imagine structure, already master page have full structure and it is add to your web page, which also have full html structure!! it will always fire error. Do one thing, create your page with use of master page. after creating your page, remove MasterPageFile from <#@ Page ..%> tag. and dynamically add masterpage.
write2varun 13-Sep-13 3:07am    
it is having every thing that is the problem, i only want the master page to come for the users

1 solution

OP:
then i will have to create two master page
i just want one master page and one aspx page
the aspx page should work normally without master page for admin
but should add master page for user
Okay, but you can't do it with one aspx.

What you can do is, put all the controls in a UserControl and include that to both the aspx page.

Now you don't need to repeat them in both the pages and it can also be used in future.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900