how to use session in asp.net

Ankitsrist
Posted by Ankitsrist under ASP.NET category on | Points: 40 | Views : 2025
hello,
lets see how to deal with session, in your login page button click event handler write the following code

//assign textbox(username) value to session, Loginname is just a session name you can give any name to it
Session["Loginname"] = TextBox1.Text;
Response.Redirect("welcome.aspx");


and if u have master page, take one label and one button(for logout, login functionality) and then write the following code in page load section
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Loginname"]!= null)
{
//if session is not null then display the name of the user
Label1.Text = "Welcome : " + Session["Loginname"];
Button1.Text = "logout";
}
else
{

Label1.Text = "Welcome: Guest!";
Button1.Text = "login here";
}

}

Comments or Responses

Login to post response