I am a fairly new ASP.NET programmer and I need some assistance with a login issue. I have code written for when someone clicks the "Login" button to log in, which works fine. While testing the application I noticed it also would log you in by simply hitting enter, but would not change the login label from "Login" to "Logout" nor the register hyperlink from "Register" to "Welcome, Username", as it does when you login using the button-click. I have session being passed to the Master page label and hyperlink upon button-click. I'll share the code and see if someone has any help for me.
Here's the Login.Aspx.cs page code:
protected void Button1_Click(object sender, EventArgs e)
{
if (UserExists(TextBoxUsername.Text, TextBoxPassword.Text))
{
Session["LoggedIn"] = TextBoxUsername.Text;
Response.Redirect("~/Home.aspx");
}
if (CompanyUserExists(TextBoxUsername.Text, TextBoxPassword.Text))
{
Session["LoggedIn"] = TextBoxUsername.Text;
Response.Redirect("~/Home.aspx");
}
else
{
LabelLoginMessage.Text = "Your credentials aren't valid. Check your username & password spelling, or click above to register now.";
}
}
}
Here's the MasterPage.master.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
PanelLogin.Visible = false;
PanelLogout.Visible = false;
if (Session["LoggedIn"] != null)
{
PanelLogout.Visible = true;
LinkButtonLogout.Text = "Logout";
HyperLinkRegister.Text = ("Welcome, " + Session["LoggedIn"].ToString());
HyperLinkRegister.NavigateUrl = "~/Home.aspx";
}
if (Session["LoggedIn"] == null)
{
PanelLogin.Visible = true;
LinkButtonLogin.Text = "Login";
HyperLinkRegister.Text = "Register";
HyperLinkRegister.NavigateUrl = "~/Register.aspx";
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session.Abandon();
LinkButtonLogin.Text = "Login";
PanelLogout.Visible = false;
PanelLogin.Visible = true;
}
}