Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘Cookies’

Use of Cookies in jQuery

Posted by Viral Sarvaiya on September 27, 2013


I have already writen that how to use cookies in asp.net and silverlight from below post
https://codesimplified.com/2009/09/10/how-to-createreaddelete-cookies-in-asp-net/
and
https://codesimplified.com/2010/08/27/cookies-in-silverlight/

here i am posting new post that shows how to use cookies in Jquery

Create cookie:

$.cookie('myCookie', 'CookieValue', { expires: 7, path: '/' });

Read cookie:

$.cookie('myCookie'); // => "CookieValue"

Read all available cookies:

$.cookie(); // => { "myCookie": "CookieValue", "...remaining": "cookies" }

Delete cookie:

// Returns true when cookie was found, false when no cookie was found...
$.removeCookie('myCookie');

// Same path as when the cookie was written...
$.removeCookie('myCookie', { path: '/' });

Thanks.

Advertisements

Posted in .Net, ASP.NET, Javascript, Jquery | Tagged: , , , , , | Leave a Comment »

Cookies in silverlight

Posted by Viral Sarvaiya on August 27, 2010


hello friends…

here i demonstrate how to set the cookies and get the values of the cookies and set the values of the cookies in the silverlight. for that i am making 2 function named GetCookie() and SetCookie().

in my project i have 2 textbox for the key and the value of the cookies then 1 button that set the value of the cookies by the key and then one button that show the cookies value by the key.

Note : For using the cookie, you have to add namespance “System.Windows.Browser”.

Step 1 : Create new silverlight project.

Step 2 : in mainpage.xaml file


<UserControl x:Class="SilverlightCookies.MainPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 <Grid x:Name="LayoutRoot" ShowGridLines="True">
 <Grid.ColumnDefinitions>
 <ColumnDefinition Width="400"></ColumnDefinition>
 <ColumnDefinition Width="200"></ColumnDefinition>
 </Grid.ColumnDefinitions>
 <Grid.RowDefinitions>
 <RowDefinition Height="200"></RowDefinition>
 <RowDefinition Height="200"></RowDefinition>
 <RowDefinition Height="200"></RowDefinition>
 </Grid.RowDefinitions>

 <TextBlock Name="lblKey" Text="Key : " Grid.Column="0" Height="30" Grid.Row="0"></TextBlock>
 <TextBox Name="txtCookieskey" Grid.Column="0" Grid.Row="0" Width="200" Height="30" Margin="0,0,0,0"></TextBox>
 <TextBlock Name="lblValue" Text="Value : " Grid.Column="0" Height="30" Grid.Row="0" Margin="0,70,0,0"></TextBlock>
 <TextBox Name="txtCookiesValue" Grid.Column="0" Grid.Row="0" Width="200" Height="30" Margin="0,70,0,0"></TextBox>
 <Button Name="btnSetCookies" Grid.Column="0" Grid.Row="1" Content="Set Cookies" Width="100" Height="30" Click="Button_Click"></Button>
 <TextBlock Name="LblCookies" Grid.Column="0" Grid.Row="2"></TextBlock>

 <Button Name="btnGetCookies" Grid.Column="1" Grid.Row="0" Content="Get Cookies" Width="100" Height="30" Click="Button_Click_1"></Button>
 <TextBlock Name="LblDisplayCookies" Grid.Column="1" Grid.Row="1" Width="auto" Height="auto"></TextBlock>

 </Grid>
</UserControl>

Step 3 : my mainpage.xaml.cs file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
namespace SilverlightCookies
{
 public partial class MainPage : UserControl
 {
 public MainPage()
 {
 InitializeComponent();
 }

 private void SetCookie(string key, string value)
 {

 DateTime expireDate = DateTime.Now + TimeSpan.FromHours(1);
 string newCookie = key + "=" + value + ";expires=" + expireDate.ToString("R");
 HtmlPage.Document.SetProperty("cookie", newCookie);
 }

 private string GetCookie(string key)
 {
 string[] cookies = HtmlPage.Document.Cookies.Split(';');
 foreach (string cookie in cookies)
 {
 string[] keyValue = cookie.Split('=');
 if (keyValue.Length == 2)
 {
 if (keyValue[0].ToString().Trim() == key.Trim())
 return keyValue[1];
 }
 }
 return null;
 }

 private void Button_Click(object sender, RoutedEventArgs e)
 {
 SetCookie(txtCookieskey.Text.Trim(), txtCookiesValue.Text.Trim());
 LblCookies.Text = "Cookes has been Created";
 }

 private void Button_Click_1(object sender, RoutedEventArgs e)
 {
 LblDisplayCookies.Text = GetCookie(txtCookieskey.Text.Trim());
 LblCookies.Text = "Display Cookies of " + txtCookieskey.Text;
 }
 }
}

Step 4 : Run the application…

for this you get 2 textbox and 2 button.

when i input the key and value in the textbox and press button of “Set Cookies”, it set the key of cookies by that value and when i click on “get Cookies” by typing key value in key textbox, it show me the value of the key in cookie.

Enjoy Coding……..

Posted in ASP.NET, Silverlight | Tagged: , , , | 2 Comments »

How to create,Read,Delete Cookies in Asp.net

Posted by Viral Sarvaiya on September 10, 2009


A cookie is a small bit of text file that browser creates and stores on your machine (hard drive). Cookie is a small piece of information stored as a string. Web server sends the cookie and browser stores it, next time server returns that cookie.Cookies are mostly used to store the information about the user. Cookies are stores on the client side.

Here i m going to explain you by providing example of Remember me Code :

Step 1 : if check box is checked for “Remember Me” then create cookie else Delete it.

if (chkRememberMe.Checked == true)
{
//Create Cookie to Store AdminInfo
HttpCookie aCookie = new HttpCookie("AdminInfo");
aCookie.Values["userName"] = txtUsername.Text;
aCookie.Values["Password"] = txtPassword.Text;
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(aCookie);
}
else
{
//Delete Cookie
HttpCookie aCookie = new HttpCookie("AdminInfo");
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}

Step 2 : now check cookie is null or not in page load event & set username & password from cookie

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["AdminInfo"] != null)
{
txtUsername.Text = Request.Cookies["AdminInfo"]["userName"] == null ? null : Request.Cookies["AdminInfo"]["userName"].ToString();
string pwd = Request.Cookies["AdminInfo"]["Password"] == null ? null : Request.Cookies["AdminInfo"]["Password"].ToString();
txtPassword.Attributes.Add("value", pwd);
}
}

}

Posted in ASP.NET | Tagged: , , | 2 Comments »

 
%d bloggers like this: