Click here to Skip to main content
15,894,252 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
my code for the login page
PLZ help me to display the login user name and also enable the logout link

Simple Login page

ASP
    <html xmlns="http://www.w3.org/1999/xhtml">
<br />
<head>
 <h2>
        Log In
    </h2>
    <p>
        Please enter your username and password.
        <asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="False" 
            ForeColor="Blue" NavigateUrl="~/register.aspx">Register if you don't have an account.
    </p>
<div class="accountInfo">
                <fieldset class="login">
                    <legend>Account Information</legend>
<title>Login Form</title>
</head>
<body>
<div>
<table>
<tr>
<td>
Username:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"/>
<asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password"/>
<asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</body>
</fieldset></div>
</html>

site master page
ASP
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="True">
    <anonymoustemplate>
        [ <a href="~/Lg1.aspx" ID="HeadLoginStatus"  runat="server">Member Log In ]
    </anonymoustemplate>
    <loggedintemplate>
        <asp:LoginName ID="LoginName1" FormatString="Welcome {0}!" runat="server" />
        [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
    </loggedintemplate>


CS file
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Lg1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from User_information where UserName =@username and Password=@password",con);
cmd.Parameters.AddWithValue("@username", txtUserName.Text);
cmd.Parameters.AddWithValue("@password", txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
Response.Redirect("Default.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
    }
Posted
Updated 27-Apr-20 4:21am
v2
Comments
Sandeep Mewara 14-Mar-13 8:52am    
Your question is not quite clear. Can you please rephrase and share what is the issue?
programmerbreakkid 14-Mar-13 9:51am    
when clicked on login it logs in the user and redirects the user to the home page but the user name s not displayed and also the login button stays the same login it should actually be logout
Richard C Bishop 14-Mar-13 8:53am    
You need to ask your question in the body of the thread and not in the header line. We cannot read what it says completely which will reduce the chances of you getting help.
programmerbreakkid 14-Mar-13 9:53am    
when clicked on login it logs in the user after validating from sql server and redirects the user to the home page but the user name s not displayed on the page and also the login button stays the same login it will not change to logout... only if logout is enabled user can lougout
CHill60 14-Mar-13 10:17am    
Are you saying that the user name and logout button should appear on the home page or are you loading the login page again?

1 solution

your browser catch the page so you can do this . .

1:: Response.Cache.SetCacheability(HttpCacheability.NoCache); 'put this line in all page load event'

2 :: you have to reload page after login and logut

try this may work for you
 
Share this answer
 
v2
Comments
programmerbreakkid 14-Mar-13 9:48am    
when clicked on login it logs in the user and redirects the user to the home page but the user name s not displayed and also the login button stays the same login it should actually be logout
Maciej Los 17-Mar-13 13:54pm    
Comment from Archit Patel:
you have to give whole code than i can give you good ans .. :)

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