Click here to Skip to main content
14,735,693 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

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 21: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
Harshil_Raval 13-Sep-13 3:10am
   
Ok.Same page is used for both admin and user. For admin it does not require master page to add in this page, but for user master page must be add to this page. Is this your requirement?
write2varun 13-Sep-13 3:11am
   
yes
Harshil_Raval 13-Sep-13 3:14am
   
Sorry, varun but it is not possible. You can not handle this in single page.
write2varun 13-Sep-13 3:18am
   
there must be some solution??? :(
Harshil_Raval 13-Sep-13 3:23am
   
Sorry, you have to change your logic, if you want to use same page, you must create master page for you admin too and add master page dynamically accordingly logged in user. This is the only solution, or create two different page.
Dev Krish 13-Sep-13 2:56am
   
Can u please post the exception
write2varun 13-Sep-13 3:00am
   
"Content controls have to be top-level controls in a content page or a nested master page that references a master page"
write2varun 13-Sep-13 3:01am
   
i tried to pt the content controller i the page then it throws the error
"ContentPlaceHolder can only be used in .master files."
Dev Krish 13-Sep-13 3:01am
   
Hey have you created a page using a master page or just simply created a page.
write2varun 13-Sep-13 3:06am
   
simply created it
Dev Krish 13-Sep-13 3:10am
   
you can solve your problem in two ways:
1. Create a page using master page
2. Add a tag to you file MasterPageFile="~/MASTER_PAGE_PATH" in aspx page using help of this link "http://stackoverflow.com/questions/5549638/how-to-add-master-page-to-already-created-webform"
write2varun 13-Sep-13 3:14am
   
cant do that, i dont have to use the master page when i am logging as admin
Dev Krish 13-Sep-13 3:15am
   
Then you can use session_start event to redirect to user or admin page.
write2varun 13-Sep-13 3:17am
   
i want to use the same page for both the users
Dev Krish 13-Sep-13 3:33am
   
You can't use both at a time.
write2varun 13-Sep-13 3:43am
   
that sucks...!!! :(
   
Are you sure the path "~/NewMaster.master" is correct?
write2varun 13-Sep-13 5:05am
   
yes
   
That means your Master Page is present one folder up, right?

Can you describe the folder structure?
write2varun 13-Sep-13 6:51am
   
it is not inside a folder
   
Yeah I got. Check How to assign a master page to a existing .aspx page?. Have you done all these mentioned steps?

Otherwise it will not work.
write2varun 13-Sep-13 7:09am
   
i cant do those things
write2varun 13-Sep-13 7:31am
   
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 add that to both the aspx page, so that you don't need to repeat them in both the pages and it can be used in future.

Makes sense?
write2varun 13-Sep-13 8:03am
   
ya i got that so there is no way that i can do that using only one page
   
No, not at all. I have added one answer. Please accept and up-vote.

Thanks,
Tadit
write2varun 13-Sep-13 8:11am
   
i cant accept as it as the answer this is not solving my problem
   
As I said, it is not possible and you have to create two aspx pages.

To avoid repetition of controls, I suggested you to create a UserControl. That is a work-round, which I can think of.

Accepting the answer is your decision, I am not bothering you. :)
write2varun 14-Sep-13 0:46am
   
okay fine
   
Thanks for accepting the answer buddy... :)
write2varun 14-Sep-13 8:19am
   
no problem

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.

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