Asp.net modules
  • Home
  • c# Modules
  • Web Design
    • Html
    • Css
    • Java Script
  • Bootstrap
  • Sql
    • Queries
    • Stored Procedures
  • About Me
    • About Myself
    • My Projects
    • My Resume
  • Photo Gallery

Sunday, September 27, 2015

How to use Session in ASP.net

 Unknown     2:57 AM     Asp.net     No comments   


Introduction:


Hi.. Friends,

Today I explain that how to use session in asp.net so here i cover all events of session step by step now i going to elaborate this.


Step:1         The designing code goes here.


<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>How to use session in ASP.net</title>

    <style type="text/css">

        .txtInput {

            width: 250px;

            height: 28px;
            padding: 3px;
        }

        div {
            margin: 5px;
        }

        .validator {
            color: Red;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="height: 281px; width: 1128px">
                <h3 style="color: #249fa3">How to use Session in ASP.net</h3>
                <div style="color: #249fa3; font-weight: bold">
                    Userame:<br />
                    <br />
                    <asp:TextBox ID="txtUserName" runat="server" MaxLength="50" Height="29px" Width="164px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvFirstName" runat="server" Display="Dynamic"
                        CssClass="validator" ControlToValidate="txtUserName"
                        ErrorMessage="Username is required"></asp:RequiredFieldValidator>
                    <br />
                </div>
                <div>
                    <asp:Button ID="btnCreateSession" runat="server"
                        Text="Click to Create Session" OnClick="btnCreateSession_Click" BackColor="#249FA3" BorderColor="White" Font-Bold="True" ForeColor="White" Height="38px" />
                    &nbsp;
               
                    <asp:Button ID="btnRetrieveSession" runat="server" CausesValidation="false"
                        Text="Click to Retrieve Session Value" OnClick="btnRetrieveSession_Click" BackColor="#249FA3" BorderColor="White" Font-Bold="True" ForeColor="White" Height="38px" />
                    &nbsp;
               
                    <asp:Button ID="btnRemoveSession" runat="server" CausesValidation="false"
                        Text="Click to Remove Session Value" OnClick="btnRemoveSession_Click" BackColor="#249FA3" BorderColor="White" Font-Bold="True" ForeColor="White" Height="38px" />
                    &nbsp;
               
                    <asp:Button ID="btnRemoveAll" runat="server" CausesValidation="false"
                        Text="Click to Remove All Sessions" OnClick="btnRemoveAll_Click" BackColor="#249FA3" BorderColor="White" Font-Bold="True" ForeColor="White" Height="38px" />
                </div>
                <p style="color: #249fa3">
                    Note: Firstly create a session by providing user name in text field, then you can retrieve the value from session.
                </p>
                <div style="color: #249fa3">
                    Value stored in Session: 
               
                    <strong>
                        <asp:Label ID="lblSessionValue" runat="server"></asp:Label></strong>
                </div>
            </fieldset>
        </div>
    </form>
</body>
</html>



 Step:2     Add some header files as i given blow.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



Step:3   The CS code goes here

public partial class index : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnCreateSession_Click(object sender, EventArgs e)
    {
        Session["Username"] = txtUserName.Text.Trim();
    }

    protected void btnRetrieveSession_Click(object sender, EventArgs e)
    {
        DisplaySessionValue();
    }

    protected void btnRemoveSession_Click(object sender, EventArgs e)
    {
        Session.Remove("Username");

        DisplaySessionValue();
    }

    protected void btnRemoveAll_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();

        DisplaySessionValue();
    }

    private void DisplaySessionValue()
    {
        if (Session["Username"] != null)
            lblSessionValue.Text = Convert.ToString(Session["Username"]);
        else
            lblSessionValue.Text = "No Value has been stored in session";
    }
}




Demo :















  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Popular Posts

  • Asp.net insert, Edit, update, delete in grid view
    Introduction :   Here I explain how to insert, edit, update and delete data in grid view using asp.net. In this project I use so...
  • How to make a hit-dot game in Asp.net using Java-Script
        Introduction: Hi.. Friends, Today I explain that how to make a game in asp-dot net without using any database. Its so simple ...
  • Create a Cart Popup div in HTML & CSS
    Intoduction :-   Hii Friends... Today I explain how to Create a Cart Popup div in HTML & CSS . This moduel base on mouse hover when y...
  • How to create pikachu character using HTML & CSS
    Hii Friends... Today I explain how to create cartoon( Pikachu character) using HTML and CSS . you can use this code in your project cop...
  • Auto-Hiding Navbar with Javascript
    Intoduction :-   Hii Friends... today i explain how to hide a navbar when scroll the page. Fixed headers are fairly common nowadays with...
  • Angular Expressions
    Intoduction :-   Hii Friends... Today I explain about angular js expressions. this is similar to JavaScript expressions. 1) The expr...
  • How To scroll from bottom to top
    Intoduction :- Hii Friends...   Today I explain How To scroll from top to bottom and bottom to top, we use “scrollTop” function of jque...
  • How to create Animation Using HTML & CSS
    Hii Friends... Today I explain how to create Animations using HTML and CSS . you can use this code in your project copy this code and p...
  • How To Make Semi-Transparent Buttons
    Intoduction :-   Hii Friends...   Today I explain how to make Semi-Transparent Buttons with the help of css. This style now a days goes t...
  • List Style
    Intoduction :-   Hii Friends... Html have 2 types of list . Ordered List and Unordered List. If you are required to put your items in a n...

Blog Archive

  • ►  2017 (13)
    • ►  December (1)
    • ►  March (5)
    • ►  February (1)
    • ►  January (6)
  • ►  2016 (31)
    • ►  November (1)
    • ►  September (1)
    • ►  June (6)
    • ►  May (3)
    • ►  April (2)
    • ►  March (8)
    • ►  February (9)
    • ►  January (1)
  • ▼  2015 (31)
    • ►  December (3)
    • ►  November (4)
    • ►  October (18)
    • ▼  September (6)
      • How to make a hit-dot game in Asp.net using Java-S...
      • How to use Session in ASP.net
      • How to find results by click on dropdownlist selec...
      • How To Bind A Repeater In Asp.Net
      • How To Make A Responsive Grid System
      • Asp.net insert, Edit, update, delete in grid view

About Me

Unknown
View my complete profile

Copyright © Asp.net modules | Powered by Blogger
Design by Sheetal Khandelwal