How to edit Master Page
-
HiEveryone!!! I'm using Master page in my website.... When the user logged in, the asp menu button of the master page should be enabled or disabled according to the user permission.... How to do this? Plz Reply!!!!!!!! I Cannot able to access the controls of Master Page in Code Behind.. How to access those controls and edit the properties of those controls???
-
HiEveryone!!! I'm using Master page in my website.... When the user logged in, the asp menu button of the master page should be enabled or disabled according to the user permission.... How to do this? Plz Reply!!!!!!!! I Cannot able to access the controls of Master Page in Code Behind.. How to access those controls and edit the properties of those controls???
Shalini_U wrote:
How to do this? Plz Reply!!!!!!!!
With code.
Shalini_U wrote:
I Cannot able to access the controls of Master Page in Code Behind..
Yes you can. THe Master property, returns a MasterPage. What you need, is to upcast that to an instance of your master page class. Although, I think you should be able to handle all this permissions stuff inside the master page class itself, what I do if I ever use master page, is create a new base class for all my pages, which has a strongly typed MasterPage property that returns the upcast Master page. Then I can access the methods however I want. Of course, not being a hack programmer, I never make the controls on the master page public, I use methods and properties to allow only the required access.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Shalini_U wrote:
How to do this? Plz Reply!!!!!!!!
With code.
Shalini_U wrote:
I Cannot able to access the controls of Master Page in Code Behind..
Yes you can. THe Master property, returns a MasterPage. What you need, is to upcast that to an instance of your master page class. Although, I think you should be able to handle all this permissions stuff inside the master page class itself, what I do if I ever use master page, is create a new base class for all my pages, which has a strongly typed MasterPage property that returns the upcast Master page. Then I can access the methods however I want. Of course, not being a hack programmer, I never make the controls on the master page public, I use methods and properties to allow only the required access.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Permission Means, just to check whether the user is enabled for a particular feature... So that the particular button is enabled for him... for this, I'll get the information abt the user whether the user is enabled or not from the database table. Then , I hav to set that particular button to enabled or disable. When I'm tried to get the control of Master Page, I can't do this in code behind... Is any other way to achiive this??
-
Permission Means, just to check whether the user is enabled for a particular feature... So that the particular button is enabled for him... for this, I'll get the information abt the user whether the user is enabled or not from the database table. Then , I hav to set that particular button to enabled or disable. When I'm tried to get the control of Master Page, I can't do this in code behind... Is any other way to achiive this??
Shalini_U wrote:
When I'm tried to get the control of Master Page, I can't do this in code behind... Is any other way to achiive this??
I guess you need to read my answer again, I explained exactly how to do it. And, as I said, the session should store details of who is logged in, so why can't your master page check for itself what it should show as a result ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Permission Means, just to check whether the user is enabled for a particular feature... So that the particular button is enabled for him... for this, I'll get the information abt the user whether the user is enabled or not from the database table. Then , I hav to set that particular button to enabled or disable. When I'm tried to get the control of Master Page, I can't do this in code behind... Is any other way to achiive this??
if You are Using forms authentication
//I did it for Image Button replace it for required menuitem
if (Page.User.Identity.IsAuthenticated)
{
//logout
((Image)Master.FindControl("ImageButtonLogin")).Visible = false;//login ImageButton
((Image)Master.FindControl("ImageButtonLogout")).Visible = true;//logout ImageButton}
If this is not you are looking for sorry..!
MyFirstArticlePublished: MenuControlSelectedItem Why Do Some People Forget To Mark as Answer .If It Helps.
-
if You are Using forms authentication
//I did it for Image Button replace it for required menuitem
if (Page.User.Identity.IsAuthenticated)
{
//logout
((Image)Master.FindControl("ImageButtonLogin")).Visible = false;//login ImageButton
((Image)Master.FindControl("ImageButtonLogout")).Visible = true;//logout ImageButton}
If this is not you are looking for sorry..!
MyFirstArticlePublished: MenuControlSelectedItem Why Do Some People Forget To Mark as Answer .If It Helps.
Yes, this sort of thing will work, but it's ugly. Creating a strongly typed property is a far nicer solution IMO.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Permission Means, just to check whether the user is enabled for a particular feature... So that the particular button is enabled for him... for this, I'll get the information abt the user whether the user is enabled or not from the database table. Then , I hav to set that particular button to enabled or disable. When I'm tried to get the control of Master Page, I can't do this in code behind... Is any other way to achiive this??
Shalini_U wrote:
I can't do this in code behind... Is any other way to achiive this??
Ofcourse you can. I think you didn't understand what CG told you to do. Thats the best option, but if still you need code then try this:
HtmlForm frm = (HtmlForm)Page.Master.FindControl("form_id");
Menu mnu = (Menu)frm.FindControl("menu_id");
mnu.Enabled = false; -
Shalini_U wrote:
When I'm tried to get the control of Master Page, I can't do this in code behind... Is any other way to achiive this??
I guess you need to read my answer again, I explained exactly how to do it. And, as I said, the session should store details of who is logged in, so why can't your master page check for itself what it should show as a result ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Shalini_U wrote:
I can't do this in code behind... Is any other way to achiive this??
Ofcourse you can. I think you didn't understand what CG told you to do. Thats the best option, but if still you need code then try this:
HtmlForm frm = (HtmlForm)Page.Master.FindControl("form_id");
Menu mnu = (Menu)frm.FindControl("menu_id");
mnu.Enabled = false; -
HiEveryone!!! I'm using Master page in my website.... When the user logged in, the asp menu button of the master page should be enabled or disabled according to the user permission.... How to do this? Plz Reply!!!!!!!! I Cannot able to access the controls of Master Page in Code Behind.. How to access those controls and edit the properties of those controls???
you can use like this... if(UserHasPermission) { Control ctr=this.Master.Findcontrol("ControlID") as Control; ctr.Enabeled=True; }
-
Hi! Thank U..... I used that Code... But, error occurs that "Object Reference Not set to an instance of Object"
where did error occurred? Can you show me your code?
-
where did error occurred? Can you show me your code?
-
MenuItem mnu = (MenuItem)frm.FindControl("menuitem1"); This shows error..... I used this code now.. Menu1.Items[3].Enabled = true; It's working for me......... Thank you for ur idea...
Its good that you worked it out but this is not a good way to achieve it. It you can, try to implement the way told above. :)