My logout button's code looks like this:
protected void ButtonLogout_Click(object sender, EventArgs e)
{
Session["login"] = null;
}
And my page's onLoad event looks like this:
protected void Page_Load(object sender, EventArgs e)
{
if ((string)Session["login"] != null)
{
ButtonLogout.Visible = true;
// (...)
}
else
{
ButtonLogout.Visible = false;
// (...)
}
}
I'm having problem with this thing working properly. I didn't know what's going on so I've setted up break points in both ButtonClick and PageLoad events. What I found out is that PageLoad event executes first (sic!) and that's why ButtonLogout wasnt hiding after clicking it. I could simply refresh page in ButtonClick event but I'm not sure whether Loading Page twice after 1 button click is OK. Isnt there any other way to solve this?
Thanks.
The issue is that the page life cycle executes in a specific way. In basic terms it goes:
Page_Load
Events fire
Pre_Render
More details can be found - http://msdn.microsoft.com/en-us/library/ms178472.ASPx
If you're setting the visibility of a control you can set this in the Pre_Render page event and at that point the button event will have fired and set what you require.
I recommend you to use asp.net LoginStatus to handle that. And dont forget to clear the session when the user logs out. Checking Using LoginName and LoginStatus Controls will help you.
protected void HeadLoginStatus_LoggingOut(object sender, LoginCancelEventArgs e)
{
Session.Clear();//It clears the session when the user logged out.
Session.Abandon();
FormsAuthentication.SignOut();
}
Related
I want to load a dictionary with some data and use it in the button event handler of a web form. Where is the appropriate place to put this code? There is a Page_Load method, but I don't want it to run every time the page loads.
in a Page Load event handler, call the method only once when the page first loads.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// your code to load dictionary here, or a method call.
}
}
I have C# Web Application that has an aspx page hosting a user control (Review.ascx). Inside that user control there are 5 more user controls, one of which has a public event (Review_Summary.ascx). The problem is no matter what i do I cannot get the event wired up in the parent ascx control (Review.ascx).
Here is what I have in the child control (Review_Summary.ascx)
public event EventHandler forwardStatusChanged;
#region methods
protected void btnForward_Click(object sender, EventArgs e)
{
if (btnForward.Text == "Return")
{
if (forwardStatusChanged != null)
{
forwardStatusChanged(sender, e);
}
removeForward();
}
}
In the parent control (Review.ascx) I have this
public void initReview(string EmployeeNumber)
{
RevSummary.forwardStatusChanged += new EventHandler(RevSummary_forwardStatusChanged);
<more code here>
}
protected void RevSummary_forwardStatusChanged(object sender, EventArgs e)
{
lblReadOnly.Visible = false;
}
RevSummary is the ID of the child control in the parent control. InitReveiw is a method that is called by the aspx page in its Page_Load event.
I get no errors on compile or at runtime. But when I click the button the forwardStatusChanged event is null. The "removeForward()" method that is called after that executes properly. So that fact that the event is always null leads me to believe that the wire up in the parent control is not working. However, I am sure it is executing becasue all of the code after that executes.
How can I figure out why this event is not wiring up?
Where is initReview being called from? Are you sure it's being called because the only reason this happens is that the event handler wasn't truly setup. I've never found a reason other than this, the several times I did this myself.
HTH.
I need to logout on close page or on close browser ...
as usual ...
but by default ASP Membership wont do it ...
How to make logout when I just leave my site
(FormsAuthentication.SignOut();
HttpContext.Current.Session.Abandon();)
btw got problem on logout button.
here is a code on page render to check if user authorization=true I setup "authorezated panel with logout button"
protected void Page_PreRender()
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Label1.Text = System.Web.HttpContext.Current.User.Identity.Name;
MultiView1.ActiveViewIndex = 0;
}
else
{
MultiView1.ActiveViewIndex = 1;
}
}
but when I click logout
protected void Button2_Click(object sender, System.EventArgs e) //logout
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
System.Web.Security.FormsAuthentication.SignOut();
System.Web.HttpContext.Current.Session.Abandon();
MultiView1.ActiveViewIndex = 1;
}
}
Page rendering before logout and I can't see ActiveViewIndex = 1 :(
So I need to click twice on logout button . weird.
There is no request sent to the server when you close a page. There is only a javascript event that fires. So you would have to do a ajax request to do what you need. But I wouldn't depend on that. But if you really need to sign out your user when he closes the page, then that is what you have to do. You can do something like this (if you use jquery):
window.onunload = logOut;
function logOut() {
$.get("[[url to a resource that logs you out]]");
}
About the second problem, I would suggest put the code in your Page_PreRender() in Page_Load() instead.
Can I bubble up a button click event of a button in master page to be handled by an event handler in the aspx page ?
You can expose the event handler and hookup to it, like this:
In the master:
public event EventHandler ButtonClick
{
add { ButtonThatGetsClicked.Click += value; }
remove { ButtonThatGetsClicked.Click -= value; }
}
In the page:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
((MyMasterType)Master).ButtonClick += MyHandler;
}
private void MyHandler(object sender, EventArgs e)
{
//Do Something
}
Also, you can avoid the Master type cast and have it already appear in intellisense as your Master's type by using the #MasterType directive in the aspx markup.
You can rebroadcast the event. Declare a new corresponding event in your master page, such as HelpClicked and then aspx pages that use this master can subscribe to the event and handle it appropriately. The master can also take a default action if there are no subscribers (or use an EventArgs with a Handled property or something like that).
I need to perform a few checks (enable or disable a label elsewhere on the page) after the user Inserts or Cancels the DetailsView. I have to do this because the DropDownList.SelectedIndexChanged doesn't fire during this event (which is dumb in my opinion, since the index DOES change).
ModeChanging should fire, so can you do this?
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (e.CancelingEdit)
{
//Checks
}
}