Set Logout Button inside master page visible after successfully Login in ASP.Net

Last Reply 4 days ago By pandeyism

Posted 4 days ago

Hi 

guys,

I am using login & logout link in master pgae using bootstrap.

Now i want Logout link is activated only when login successfully done otherwise logout link disable or not visible.

Posted 4 days ago

Hi hnmahant,

Please refer below sample.

HTML

MasterPage.master

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton ID="lbLogout" runat="server" Text="LogOut" Visible="false" OnClick="LogOut"></asp:LinkButton>
        <hr />
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Login.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    Name:
    <asp:TextBox ID="txtName" runat="server">
    </asp:TextBox>
    <br />
    Password:
    <asp:TextBox ID="txtPassword" TextMode="Password" runat="server">
    </asp:TextBox>
    <br />
    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
</asp:Content>

Home.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    Home Page.
</asp:Content>

Code

C#

MasterPage.master.cs

protected void LogOut(object sender, EventArgs e)
{
    Response.Redirect("Login.aspx");
}

Login.aspx.cs

protected void btnLogin_Click(object sender, EventArgs e)
{
    if (txtName.Text == "admin" && txtPassword.Text == "admin")
    {
        Response.Redirect("Home.aspx");
    }
}

Home.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    LinkButton lnkLogout = this.Master.FindControl("lbLogout") as LinkButton;
    lnkLogout.Visible = true;
}

VB.Net

MasterPage.master.vb

Protected Sub LogOut(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect("Login.aspx")
End Sub

Login.aspx.vb

Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs)
    If txtName.Text = "admin" AndAlso txtPassword.Text = "admin" Then
        Response.Redirect("Home.aspx")
    End If
End Sub

Home.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim lnkLogout As LinkButton = TryCast(Me.Master.FindControl("lbLogout"), LinkButton)
    lnkLogout.Visible = True
End Sub

Screenshot