Tuesday, 29 May 2012

Set Login name and Logout button on master page 

Step 1) First we create a master page and add two link button as Login and Logout
Code for Master page: Master.aspx
<div class="loginDisplay">
                <asp:Label ID="Loginname" runat="server"></asp:Label><br />             
                <asp:LinkButton ID="link1" runat="server" Text="Logout" Visible="False"
                        onclick="link1_Click"></asp:LinkButton> 
            </div>
Master.cs code:
      protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["ll"] == null)
            {
               
            }
        }
        }
    protected void link1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx");
    }
step 2: Create a Login Page
Login.aspx  code
<fieldset style="width:400px; height:200px;">
<legend>Login</legend>
<table style="height: 156px; width: 340px">
<tr>
<td colspan="2">
<asp:Label ID="lblmsg" runat="server" CssClass="label" Height="16px" Width="273px"></asp:Label>
</td>
</tr>
<tr>
<td>User Id <span class="style1">*</span></td>
<td><asp:TextBox ID="txtuserid" runat="server" Height="22px" Width="180px"></asp:TextBox></td>
</tr>
<tr>
<td>Password <span class="style2">*</span></td>
<td><asp:TextBox ID="txtpass" runat="server" Height="22px" Width="180px" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnlogin" runat="server" Text="Login" BackColor="#660066"
        Font-Bold="True" ForeColor="White" onclick="btnlogin_Click" /></td>
</tr>
<tr>
<td colspan="2">
&nbsp;Forgot Password?</td>
</tr>
</table>
</fieldset>


Login.cs code

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new  
    SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
    SqlDataAdapter da;
    SqlCommand cmd;
    SqlDataReader dr;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("sklogin_ins", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", txtuserid.Text);
        cmd.Parameters.AddWithValue("@pass", txtpass.Text);
        cmd.ExecuteNonQuery();

        SqlDataAdapter sqlAdap = new SqlDataAdapter(cmd);
        ds = new DataSet();
        sqlAdap.Fill(ds);

        if (ds.Tables[0].Rows.Count > 0)
        {
            Session["log"] = ds.Tables[0].Rows[0]["userid"].ToString();
            Response.Redirect("Default.aspx");
        }
        else
        {
            lblmsg.Text = "userid and password does not match";
            txtuserid.Text = "";
            txtpass.Text = "";
        }
    }
}
step 3: Now Create Default.aspx page


Default.cs code:






protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LinkButton link1 = (LinkButton)Master.FindControl("link1");
            link1.Visible = true;
            string login = Session["log"].ToString();

            Session["ll"] = login;
            Label lbl1 = (Label)Master.FindControl("Loginname");
            lbl1.Text = "hi"+" : "+" "+login.ToString();
        }
    }
 




 

No comments:

Post a Comment