Showing posts with label application. Show all posts
Showing posts with label application. Show all posts

Thursday, June 21, 2012

Cookies in asp.net : basic concept of Sessions,use of Sessions,how to use Sessions,need of Sessions in asp.net application...



Sessions can be used to store even complex data for the user just like cookies. Actually,

sessions will use cookies to store the data, unless you explicitly tell it not to. Sessions can be used easily in ASP.NET with the Session object.

We will re-use the cookie example, and use sessions instead.

Keep in mind though, that sessions will expire after a certain amount of minutes, as configured in the web.config file. Markup code:

<%@ 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>Sessions</title>
</head>
<body runat="server" id="BodyTag">
    <form id="form1" runat="server">
    <asp:DropDownList runat="server" id="ColorSelector" autopostback="true" onselectedindexchanged="ColorSelector_IndexChanged">
        <asp:ListItem value="White" selected="True">Select color...</asp:ListItem>
        <asp:ListItem value="Red">Red</asp:ListItem>
        <asp:ListItem value="Green">Green</asp:ListItem>
        <asp:ListItem value="Blue">Blue</asp:ListItem>
    </asp:DropDownList>
    </form>
</body>
</html>


And here is the CodeBehind: 

using System;
using System.Data;
using System.Web;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["BackgroundColor"] != null)
        {
            ColorSelector.SelectedValue = Session["BackgroundColor"].ToString();
            BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
        }
    }

    protected void ColorSelector_IndexChanged(object sender, EventArgs e)
    {
        BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
        Session["BackgroundColor"] = ColorSelector.SelectedValue;
    }
}


As you can see, the example doesn't need a lot of changes to use sessions instead of cookies.

Please notice that session values are tied to an instance of your browser. If you close down the browser, the saved value(s) will usually be "lost".

Also, if the webserver recycles the aspnet_wp.exe process, sessions are lost, since they are saved in memory as well.

This can be avoided by saving session states on a separate StateServer or by saving to a SQL server, but that's beyond the scope of this article.

source..

http://asp.net-tutorials.com/state/sessions/

Cookies in asp.net : basic concept of Cookies,use of Cookies,how to use Cookies,need of Cookies in asp.net application...



Cookies are small pieces of text,

stored on the client's computer to be used only by

the website setting the cookies.

This allows webapplications to save information for the user, and then re-use it on each page if needed. Here is an example where we save a users choice of background
color:


<%@ 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>Cookies</title>
</head>
<body runat="server" id="BodyTag">
    <form id="form1" runat="server">
    <asp:DropDownList runat="server" id="ColorSelector" autopostback="true" onselectedindexchanged="ColorSelector_IndexChanged">
        <asp:ListItem value="White" selected="True">Select color...</asp:ListItem>
        <asp:ListItem value="Red">Red</asp:ListItem>
        <asp:ListItem value="Green">Green</asp:ListItem>
        <asp:ListItem value="Blue">Blue</asp:ListItem>
    </asp:DropDownList>
    </form>
</body>
</html>






The page simply contains a DropDownList control, which automatically posts back each time a new item is selected.

It has 3 different colors, besides the default one, which is simply white. Once a new item is selected, the ColorSelector_IndexChanged method is fired, from our CodeBehind file:

using System;
using System.Data;
using System.Web;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.Cookies["BackgroundColor"] != null)
        {
            ColorSelector.SelectedValue = Request.Cookies["BackgroundColor"].Value;
            BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
        }
    }

    protected void ColorSelector_IndexChanged(object sender, EventArgs e)
    {
        BodyTag.Style["background-color"] = ColorSelector.SelectedValue;
        HttpCookie cookie = new HttpCookie("BackgroundColor");
        cookie.Value = ColorSelector.SelectedValue;
        cookie.Expires = DateTime.Now.AddHours(1);
        Response.SetCookie(cookie);
    }
}


Okay, two different parts to be explained here.

First, the Page_Load method, which is called on each page request. Here we check for a cookie to tell us which background color should be used.

If we find it, we set the value of our dropdown list to reflect this, as well as the background color of the page, simply by accessing the style attribute of the body tag.

Then we have the ColorSelector_IndexChanged method, which is called each time the user selects a new color.

Here we set the background color of the page, and then we create a cookie, to hold the value for us.

We allow it to expire after one hour, and then we set it by calling the SetCookie method on the Response object.

Try running the example, and set a color. Now close the browser, and start it up again. You will see that the choice of color is saved, and it will remain saved for an hour. However, nothing prevents you from saving the choice for much longer. Simply add a bigger value to the expiry date, or set an absolute value for it.





Monday, June 18, 2012

What is ASP.Net? : application codes,pages process,protocol in use..,



ASP.Net is a web development platform,

which provides a programming model, a comprehensive software infrastructure and various services required to build up robust web application

for PC, as well as mobile devices.

ASP.Net works on top of the HTTP protocol and uses the HTTP commands and policies to set a browser-to-server two-way communication and cooperation.

The ASP.Net application codes could be written in either of the following languages:

C#

Visual Basic .Net

Jscript

J#


ASP.Net Web Forms Model:

HTTP is a stateless protocol. ASP.Net framework helps in storing the information regarding the state of the application, which consists of:

Page state

Session state


Page state

The page state is the state of the client, i.e., the content of various input fields in the web form.


Session state

The session state is the collective obtained from various pages the user visited and worked with,

i.e., the overall session state. To clear the concept, let us take up an example of a shopping cart as follows.

User adds items to a shopping cart. Items are selected from a page, say the items page, and the total collected items and price are shown in a different page, say the cart page

ASP.Net session state and server side infrastructure keeps track of the information collected globally over a session.



Server side counterparts of almost all HTML elements or tags, like
and .

Server controls, which help in developing complex user-interface for example the Calendar control or the Gridview control.

The .Net framework is made of an object-oriented hierarchy.

An ASP.Net web application is made of pages. When a user requests an ASP.Net page, the IIS delegates the processing of the page to the ASP.Net runtime system.


IIS - big well known concept understand latter........