Galabin Vasilev
Galabin Vasilev

Reputation: 24

Change the buttons if user is logged

I have this navbar which shows some links on the left side and Sign in/sign up links on the right one. I want to check if the user is logged and if it is, instead of the sign in/up links, to show his name. I know how to check if there is something in the session in the code behind, but I have a hard time changing the view. Thanks in advance.

Upvotes: 0

Views: 142

Answers (2)

VDWWD
VDWWD

Reputation: 35514

You can do this. It uses the build in Request.IsAuthenticated to show/hide controls on the page. And you can use the LoginStatus Control for generating a login/logout link. See Microsoft site for more info.

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="Login.aspx">Login</asp:HyperLink>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:LoginStatus ID="LoginStatus1" runat="server" />

Code behind

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.IsAuthenticated)
    {
        HyperLink1.Visible = false;
        Label1.Text = User.Identity.Name;
    }
}

Upvotes: 1

INDIA IT TECH
INDIA IT TECH

Reputation: 1898

You can use inline code,

<% if (Session["User"] != null) { %>
//button goes here
<% } %>

Upvotes: 0

Related Questions