Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Cookies in asp.net - Part 60

Suggested Videos
Part 57 - Opening new window using javascript in asp.net
Part 58 - Techniques to send data from one webform to another
Part 59 - QueryString in asp.net

Different techniques to send data from one webform to another 
1. Cross Page Postback: Discussed in Part 55 and Part 56
2. Context.Handler object - Discussed in Part 58
3. Query strings - Discussed in Part 59
4. Cookies - Will be discussed in this session

The following concepts will be discussed in the subsequent sessions
5. Session state
6. Application state



Just like QueryStrings, Cookies can also be used to send data from one webform to another. In general, web sites use cookies to store user preferences or other information that is client-specific. Cookies store small amounts of information on the client’s machine.

Cookies can be broadly classified into 2 types
1. Persistent cookies - Remain on the client computer, even after the browser is closed. You can configure how long the cookies remain using the expires property of the HttpCookie object.
2. Non-Persistent cookies - If you don't set the Expires property, then the cookie is called as a Non-Persistent cookie. Non-Persistent cookies only remain in memory until the browser is closed. 

On WebForm1.aspx, user enters Name and Email. Let's write these values on to the client's computer using cookies. Finally read the values from the cookie and display them in WebForm2.aspx.



WebForm1.aspx HTML source:
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>
                This is WebForm1</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:TextBox ID="txtName" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:TextBox ID="txtEmail" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2"> 
            <asp:Button ID="btnSendData" runat="server" 
            Text="Go to WebForm2" onclick="btnSendData_Click" />
        </td>
    </tr>
</table>
</div>

WebForm1.aspx.cs code:
protected void btnSendData_Click(object sender, EventArgs e)
{
    // Create the cookie object
    HttpCookie cookie = new HttpCookie("UserDetails");
    cookie["Name"] = txtName.Text;
    cookie["Email"] = txtEmail.Text;
    // Cookie will be persisted for 30 days
    cookie.Expires = DateTime.Now.AddDays(30);
    // Add the cookie to the client machine
    Response.Cookies.Add(cookie);

    Response.Redirect("WebForm2.aspx");
}

WebForm2.aspx HTML Source:
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>This is WebForm2</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:Label ID="lblName" runat="server">
            </asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:Label ID="lblEmail" runat="server">
            </asp:Label>
        </td>
    </tr>
</table>
</div>

WebForm2.aspx.cs Code:
protected void Page_Load(object sender, EventArgs e)
{
    HttpCookie cookie = Request.Cookies["UserDetails"];
    if (cookie != null)
    {
        lblName.Text = cookie["Name"];
        lblEmail.Text = cookie["Email"];
    }
}

2 comments:

  1. You are doing well for Asp.Net beginner and mid level....

    Thanks
    brother

    ReplyDelete
  2. Hi Venkat,
    I just wanted to add that for the same reason that concerns the querystring (in previous lesson) it's also necessary encode (before writing) and then decode (during reading) the content of the cookies.
    So I updated the code like this:

    WebForm1.aspx.cs:
    -----------------
    // Create the cookie object
    HttpCookie cookie = new HttpCookie("UserDetails");
    cookie["Name"] = Server.UrlEncode(txtName.Text); // encoding
    cookie["Email"] = Server.UrlEncode(txtEmail.Text);
    ...

    WebForm2.aspx.cs:
    -----------------
    HttpCookie cookie = Request.Cookies["UserDetails"];
    if (cookie != null) {
    lblName.Text = Server.UrlDecode(cookie["Name"]); // decoding
    lblEmail.Text = Server.UrlDecode(cookie["Email"]);
    }

    ReplyDelete

It would be great if you can help share these free resources