How to Create Session Variable without Cookies?


sessvars.js provides this functionality. You can download this http://www.thomasfrank.se/sessvars.js
for more information about this methods like sessvars.$.clearMem(),sessvars.$.usedMem(),sessvars.$.usedMemPercent(),sessvars.$.debug() etc…. http://www.thomasfrank.se/sessionvars.html

In this example you use the textbox value in other page without cookie in javascript.

Step:1 Your first page name Default page like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="JavaScript/sessvars.js"></script>
    <script type="text/javascript">
            function PreSaveAction()
            {
                //alert(document.getElementById("txtname").value);
                sessvars.username = document.getElementById("txtname").value;
                return true;
            }

</script>

</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    Enter Your Name:<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="btnsave" type="submit" value="Save Your Name" runat="server" onclick="PreSaveAction()" onserverclick="btnsave_ServerClick" />                                                           
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</
html>

Step 2: .cs file like this

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

    }
    protected void btnsave_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("~/Default2.aspx");
    }
}


Step: 3 Your Second page where the you can reuse your textbox value

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="JavaScript/sessvars.js"></script>
    <script type="text/javascript">
        function GetUserName()
        {       
            var newusername =  sessvars.username;        
            document.getElementById("lblusername").innerHTML = newusername;
            if(sessvars.username!=undefined)
             {                  
               sessvars.$.clearMem();                           
             }
        }
    </script>
</head>
<
body>
    <form id="form1" runat="server">
    <div>   
        <asp:Label ID="lblusername" runat="server" Text=""></asp:Label>          
        <input id="btnsave" type="button" value="Displayuser" onclick="GetUserName()"  />                                                                  
    </div>
    </form>
</body>
</
html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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


Similar Articles