locked
logout using link button in asp.net c# RRS feed

  • Question

  • User186310208 posted

    i want when user click on logout its logout and it shows again Register and login option..here is my code..

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav navbar-right">
    <li><a href="#page-top" class="page-scroll">Home</a></li>
    <li><a href="#about" class="page-scroll">About</a></li>
    <li><a href="#contact" class="page-scroll">Contact</a></li>

    <div class="dropdown">
    <asp:LinkButton ID="lnkUserName" runat="server" Text="Register Her...!" OnCommand="lnkUserName_Command"></asp:LinkButton>
    <div class="dropdown-content">
    <asp:LinkButton ID="LinkButton1" runat="server" Text="Login...!" OnCommand="LinkButton1_Command"></asp:LinkButton>

    </div>
    </div>
    </ul>

    </div>


    protected void Page_Load(object sender, EventArgs e)
    {
    if(Session["UserName"]!=null)
    {
    string query = "select * from User where UserName'" + Session["UserName"] + "'";
    lnkUserName.Text = string.Format("Welcome {0}", Session["UserName"].ToString());
    LinkButton1.Text = string.Format("Logout");
    lnkUserName.PostBackUrl = "Profile.aspx";
    }



    }

    protected void lnkUserName_Command(object sender, CommandEventArgs e)
    {

    Response.Redirect("/SignIn.aspx");
    }

    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
    if (LinkButton1.Text == "Logout")
    {

    Session["UserName"] = "";
    Response.Redirect("/Walpaper.aspx");

    }

    else

    {

    Response.Redirect("/Login.aspx");
    }

    }

    Can someone help me out...!!

    Wednesday, July 29, 2020 4:24 AM

Answers

  • User186310208 posted

    It Done Sir..here is my code...

    protected void Page_Load(object sender, EventArgs e)
    {
    if(Session["UserName"]!=null)
    {
    string query = "select * from User where UserName'" + Session["UserName"] + "'";
    lnkUserName.Text = string.Format("Welcome {0}", Session["UserName"].ToString());
    LinkButton1.Text = string.Format("Logout");
    lnkUserName.PostBackUrl = "Profile.aspx";
    }



    }

    protected void lnkUserName_Command(object sender, CommandEventArgs e)
    {

    Response.Redirect("/SignIn.aspx",true);
    }

    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
    if (LinkButton1.Text == "Logout")
    {


    Session.Abandon();
    Request.Cookies.Clear();

    Response.Redirect("/Walpaper.aspx");

    }

    else

    {

    Response.Redirect("/Login.aspx");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 29, 2020 11:14 AM

All replies

  • User-939850651 posted

    Hi guestadmin,

    if(Session["UserName"]!=null)

    If you add a breakpoint in this place and use the debugging tool to run the program, you will find that it does not play a role in determining

    whether the user exists. Because an empty string is never equal to null.

    I suggest that you could modify the judgment condition as follows:

    if (Session["UserName"]!=null &&Session["UserName"].ToString()!=""){
    ..... }

    I think this can meet your requirements.

    Result:

    Best regards,

    Xudong Peng

    Wednesday, July 29, 2020 11:09 AM
  • User186310208 posted

    It Done Sir..here is my code...

    protected void Page_Load(object sender, EventArgs e)
    {
    if(Session["UserName"]!=null)
    {
    string query = "select * from User where UserName'" + Session["UserName"] + "'";
    lnkUserName.Text = string.Format("Welcome {0}", Session["UserName"].ToString());
    LinkButton1.Text = string.Format("Logout");
    lnkUserName.PostBackUrl = "Profile.aspx";
    }



    }

    protected void lnkUserName_Command(object sender, CommandEventArgs e)
    {

    Response.Redirect("/SignIn.aspx",true);
    }

    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
    if (LinkButton1.Text == "Logout")
    {


    Session.Abandon();
    Request.Cookies.Clear();

    Response.Redirect("/Walpaper.aspx");

    }

    else

    {

    Response.Redirect("/Login.aspx");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 29, 2020 11:14 AM