Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I am using master pages to manage my web application. When the application run the first page that will come is the login page which is child page of my master page. I have a button control on the master page which should be enabled after a user is logged in, so I put

C#
Button1.Enabled = false;


in the Page_Load event of the master page. Then I put the following code in the submit button of the login page so that button1 (in the master page) will be enabled and the caption of the button1 will be changed to “Logged In”

C#
Button bt;
myMasterpage  m = (myMasterpage)this.Master;
bt = m.FindControl("Button1") as Button;
bt.Enabled = true;
bt.Text = "Logged In";


Result: the text of the button is changed to “Logged In” but the button is still inactive. What do you suggest? Thanks!

Getachew
Posted
Updated 24-Jun-10 2:17am
v3

1 solution

Should this;

myMasterpage m = (myMasterpage)this.Master;

not be;
MasterPage m = (MasterPage)this.Master;

I just tried it and it does what you want if you make the change.
However, if you then Click Button1, it becomes disabled again.

Everytime the page is reloaded/posted back to the server, the button will be disabled again, to prevent this on a postback to the following in the master page load
C#
if (!this.IsPostBack)
{
    Button1.Enabled = false;
}


What you also should consider doing, is store the logged in state in the session etc. and also only conditionally set the button based on this logged in state.
 
Share this answer
 
v4
Comments
getrelax 24-Jun-10 8:25am    
I tried it but it doesn’t change the status of the Button1 to active. But as I said before the text is changed to “loged in”.
DaveAuld 24-Jun-10 8:45am    
On my system (using Chrome), the button definitely goes to Enabled, on the first click after being enabled it goes disabled again.
DaveAuld 24-Jun-10 8:52am    
Right, i have been having a play, and as i said in my answer, the problem is relating to postbacks. See revised answer.
getrelax 24-Jun-10 9:54am    
it works perfect when i use session, but not for the postback. Thanks! u saved to me alot of time and effort

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