May 22, 2014

Creating and Using Sessions in ASP.Net and C#

The Session object lets you create user sessions having a valid time before expiry , default time is 20 minutes. It has many uses and not just to create sessions , their use is typical in applications with shopping carts, a forum to establish a minimum in order to vote or rewrite a message time.



The same is stored on the server, and is administered by it. In the following example written in C # will see a precarious similación a form that loads an ArrayList Products , and sends it to the server to store it in a session variable , then opening another window even it can be retrieved and listed . The design of the first page is as follows:

<!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> </title>
</head>
<body>
    id="Form1" <form runat="server">
    <div>
 
        <table style="width:100%;">
            <tr>
                <td>
                    Product 
  </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="txtProduct" runat="server"></asp:TextBox>
                    <asp: Button ID = "btnAdd" runat = "server" onclick = "btnAdd_Click" Text = "Add"/>
                </td>
            </tr>
        </table>
 
    </div>
    <asp:HyperLink ID="HyperLink1" runat="server"
        NavigateUrl="~/Examples/Productos.aspx">HyperLink</asp:HyperLink>
    </form>
</body>
</html>

using System;
using System.Collections;
 
public partial class Examples_Sesion : System.Web.UI.Page
{
    protected void Page_Load ( object sender , EventArgs e)
    {
 
    }
    protected void btnAdd_Click ( object sender , EventArgs e)
    {
        ArrayList products;
        if ( Session["s_product"] ! = null)
            products = (ArrayList)Session["s_product"];
       else
            products = new ArrayList();
        productos.Add ( this.txtProducto.Text.ToString ());
        Session["s_product"] = product;
    }
}

On the first page we see a link to this page redirects wanted to build now

<%@Page Language="C#" AutoEventWireup="true" CodeFile="Products.aspx.cs" Inherits="Examples_Products"% >
 
<!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></title>
</head>
<body>
    id="Form1" <form runat="server">
    <div>
 
    </div >
    </form>
</body>
</html>


using System;
using System.Collections;
 
public partial class Examples_Products : System.Web.UI.Page
{
    protected void Page_Load (object sender, EventArgs e)
    {
        ArrayList items = new ArrayList();
        if ( Session["s_product"] != null)
        {
            products = (ArrayList) Session ["s_product"] ;
            foreach (string item in items)
                Response.Write(item + "<br/>");
        }
        else
        { Response.Write("No product by product"); }
    }
}

Ditulis Oleh : Angelo Hari: 4:52 PM Kategori:

0 comentarios:

Post a Comment