locked
How to call Session.Abandon() RRS feed

  • Question

  • User-259260637 posted

    I have an ASP:Hyperlink that i am using to login users to an admin area. When they login I am changing the text of the Hyperlink.

     if ((Session["userLogin"] == null) && (Convert.ToBoolean(Session["userLogin"]) != true))
            {
                lnk_admin.Text = "Admin";
                lnk_admin.NavigateUrl = "/admin/login.aspx";
            }
            else
            {
                lnk_admin.Text = "Logout";
                lnk_admin.NavigateUrl = "/";
    
                //Session.Abandon();
                //Response.Redirect(Request.RawUrl);
            }


     I wondered how could I call Session.Abandon() when the user clicks the Hyperlink when the text is Logout

    I have managed to do this by checking the url:

     else
            {
                lnk_admin.Text = "Logout";
                lnk_admin.NavigateUrl = "/";
                var path = HttpContext.Current.Request.Url.AbsolutePath;
                if (lnk_admin.Text == "Logout" && path != "/admin/edit-member.aspx")
                {
                    Session.Abandon();
                    Response.Redirect(Request.RawUrl);
                }
            }

    Monday, August 24, 2015 10:08 AM

Answers

  • User1992938117 posted

    I have an ASP:Hyperlink that i am using to login users to an admin area. When they login I am changing the text of the Hyperlink.

    Use LinkButton for the same and on click of LinkButton check the text and based on it make decision.

    protected void lnk_admin_Click(object sender, EventArgs e)
            {
                if(lnk_admin.Text =="Logout")
                {
                    Session.Abandon();
                    Response.Redirect(Request.RawUrl);
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 24, 2015 10:21 AM
  • User1724605321 posted

    Hi StudentRik,

    There is no onClick event on the Hyperlink control

    A HyperLink has no postback event when clicked .If you want to execute server code upon a click in a link, then you could use the ASP.NET control <asp:LinkButton> ,this is just like a button and will allow you to hook up Server Side Events and at the end you can just redirect the viewer to any page.

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 24, 2015 10:31 PM

All replies

  • User1992938117 posted

    I have an ASP:Hyperlink that i am using to login users to an admin area. When they login I am changing the text of the Hyperlink.

    Use LinkButton for the same and on click of LinkButton check the text and based on it make decision.

    protected void lnk_admin_Click(object sender, EventArgs e)
            {
                if(lnk_admin.Text =="Logout")
                {
                    Session.Abandon();
                    Response.Redirect(Request.RawUrl);
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 24, 2015 10:21 AM
  • User-259260637 posted

    There is no onClick event on the Hyperlink control

    Monday, August 24, 2015 10:25 AM
  • User571301025 posted

    I'm wonder why you are try with hyper link button. You have a control called "Link button" which is also displayed as a hyper link. Also it have a "onclick" event. In that you could call the Session.abondon() code. Hope this helps!

    Monday, August 24, 2015 10:18 PM
  • User1724605321 posted

    Hi StudentRik,

    There is no onClick event on the Hyperlink control

    A HyperLink has no postback event when clicked .If you want to execute server code upon a click in a link, then you could use the ASP.NET control <asp:LinkButton> ,this is just like a button and will allow you to hook up Server Side Events and at the end you can just redirect the viewer to any page.

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 24, 2015 10:31 PM
  • User1992938117 posted

    There is no onClick event on the Hyperlink control

    That's the reason i ask to use LinkButton instead of Hyperlink.

    Tuesday, August 25, 2015 4:13 AM
  • User1633621018 posted

    Hi,

    Use link button:

    <asp:LinkButton id="myLink" Text="Hi" OnClick="LinkButton_Click" runat="server"/>

    and 

    void LinkButton_Click(Object sender, EventArgs e) 
    {
       // your code here
    }
    Wednesday, August 26, 2015 5:27 PM