Click here to Skip to main content
15,896,487 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
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:
C#
//if validated user/companyuser is logged in, logout is diplayed and welcome (username) is also displayed
    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:
C#
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;
    }
}
Posted
Updated 23-Oct-12 7:03am
v3

1 solution

Hi,

You need to use the DefaultButton property of a panel to do that:
http://www.w3schools.com/aspnet/prop_webcontrol_panel_defaultbutton.asp[^]
 
Share this answer
 
Comments
JasonMacD 23-Oct-12 13:13pm    
Thank you so much, that worked perfect just was getting a lot of drawn out not concise answers through Google. Thanks buddy!
fjdiewornncalwe 23-Oct-12 14:14pm    
+5. Simple is awesome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900