Reputation: 2924
I am caught up in a strange scenario with developing a simple Login functionality in ASP.NET. There are two pages, the LoginPage.aspx
and Default.aspx
. After user provides valid credentials on Login Page, he/she is redirected to Default page. I want to display user login information on top of Default page, for that purpose I have added following HTML:
<form id="Form1" runat="server">
<div id="demo_header" runat="server">
<asp:Label ID="loggedinUsername" ForeColor="Black" runat="server"></asp:Label>
<asp:LinkButton ID="logout" runat="server" Text="Logout!" ForeColor="Black" OnClick="Logout"></asp:LinkButton>
...
</div>
</form>
And in Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["LoggedinUser"] != null)
{
loggedinUsername.Text = "Welcome " + Session["LoggedinUser"].ToString() + " ";
logout.Visible = true;
demo_header.Visible = false;
}
else
{
logout.Visible = false;
demo_header.Visible = true;
}
}
Here is the code which executes when user presses Login button on LoginPage.aspx:
protected void LoginButton_Click(object sender, EventArgs e)
{
if (LoginEmail.ToString() == string.Empty || LoginPassword.ToString() == string.Empty)
{
Session["RedirectReaasonFlag"] = "Credentials null";
login_error_msg.Text = "Please Provide Email/Password!";
}
else
{
UserStandard User = new UserStandard();
User._Email = LoginEmail.Text;
User._password = LoginPassword.Text;
Dictionary<int, string> LoggedinUserData = User.AuthenticateUser(User);
if (LoggedinUserData.Count == 1)
{
Session["LoggedinUserID"] = LoggedinUserData.ElementAt(0).Key;
Session["LoggedinUser"] = LoggedinUserData.ElementAt(0).Value;
Response.Redirect("Default.aspx");
}
else
Session["RedirectReaasonFlag"]= "Invalid Login Attempt";
}
}
But somehow when user is redirected on Default page, its name and Log out hyperlink is not shown. This is very simple task and this situation is eating up my brain. Please help me out here.
Thanks.
Upvotes: 0
Views: 492
Reputation: 129787
It looks like your loggedinUsername
label and your logout
link button are inside a div
which you are setting Visible = false
. So, you are telling the browser to hide these elements when the login is successful. Try setting demo_header.Visible = true
instead.
Upvotes: 3