server side click event doesn't fire on LinkButton when PostBackUrl property has been set to another page

StackOverflow https://stackoverflow.com/questions/16073823

Frage

I put a linkbutton control named as "Logout" on my webpage with VS 2010. When users press the "Logout" linkbutton , I want the system to do two things. First is to trigger a server side click event to do some things such clear all session variables etc.. Second is to redirect to user to another page such logon.aspx

So I wrote following codes

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        LinkButton1.PostBackUrl = "LogOn.aspx";
    }
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Session.Abandon(); 
    ....
    ....

}

But when the programs runs , any user clicks the linkbutton.The codes of LinkButton1_Click never be executed, because the page has already been redirect to Logon.aspx page and never do the LinkButton1_Click()

My Question is why LinkButton1 offers PostBackUrl property and server-side click event , but it seems that they don't cooperate very well ~

War es hilfreich?

Lösung

This is because by setting the PostBackUrl you are saying that when the button is clicked that you want it to post to the LogOn.aspx page rather than posting back to itself. Since you are posting to LogOn.aspx you will never trigger the event.

Instead, you should use some type of redirect in your button click after your sesion.abandon.

Andere Tipps

Can you check if there's an event LinkButton1_Click subscribed to a LinkButton1's OnClick?

Then for your second concern since you're referring with ASP.Net, you can handle this with your Global.asax, where you will redirect any user to a login page every time session was destroyed. See reference links below:

1.) SessionStateModule.End Event

2.) Using Session in Global.asax

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top