简体   繁体   中英

Content Page using Master Code Behind

I have a site that requires users to log in. Logging in creates a session that redirects them to the customer home page when they click the home button (instead of taking them to the non-user home page).I do this by checking if the session["Username"] has a value and session["Loggedin"] is set to true (which only happens upon logging in).

However, I can't log out once logged in. I think it has to do with the fact that my Log-out button is part of the customer.Master page that the customer home page inherits. The Log out button in the master page has code behind that clears, but when I click Log out right now, it just redirects to the customer home page. Is the master page code behind even being called?

public void Logout(object sender, EventArgs e)
{
    Session.Contents.RemoveAll();
    Session["Loggedin"] = false;
    Session["Username"] = null;
    Response.Redirect("/Pages/Default.aspx");
}

To call your master page code behind from another page, you first need to reference it on your .aspx page with the following directive (substitute with the path to your master page):

 <%@ MasterType VirtualPath="~/customer.master" %> 

You should now be able to access public members on the master page from your pages like this:

 var x = Master.myProperty; Master.myMethod(); 

EDIT
Different problem: the LinkButton has an event handler, but it's also got a HRef attribute. It's moving to a new page before it gets a chance to call the code.

Do not Href in your link button, it get's you out of the current page BEFORE executing the onclick event. That's why the code behind is not executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM