Code Simplified – Viral Sarvaiya

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

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……..

Advertisements

2 Responses to “Cookies in silverlight”

  1. Syed said

    Copy Cat!!!

  2. […] Syed on Cookies in silverlight […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: