Reading/Writing cookies with HtmlPlaceholder for Silverlight

by XAML Team | Comment 1
Recently we received a support ticket with a question whether RadHtmlPlaceholder provides support for cookies. By default, RadHtmlPlaceholder doesn't have a mechanism for reading/writing cookies. However, with the right bits of JavaScript, everything is possible :)
Imagine you have an HTML page which you want to read/write cookies to and all this has to happen from Silverlight.

First, you need few JavaScript methods for reading/writing cookies. Let's call the page we want to read/write cookies to CookiesPage.html.

<html>
    <head>
        <title>Cookies Page</title>
        <script type="text/javascript">
            // Create a cookie with the specified name and value
            function SetCookie(name, value) {
                document.cookie = name + "=" + value;
                alert("Cookie successfully set");
            }
            // Retrieve the value of the cookie with the specified name
            function GetCookie(cookieName) {
                // cookies are separated by semicolons
                var allCookies = document.cookie.split("; ");
                for (var i = 0; i < allCookies.length; i++) {
                    // a name/value pair (a crumb) is separated by an equal sign
                    var currentCookieName = allCookies[i].split("=");
                    if (cookieName == currentCookieName[0])
                        return allCookies[i];
                }
                // a cookie with the requested name does not exist
                return null;
            }
            // Retrieve all cookies
            function GetAllCookies() {
                var allCookies = document.cookie.split("; ");
                return allCookies;
            }
        </script>
    </head>
    <body bgcolor="aqua">
        <h1>Cookies page</h1>
    </body>

</html>

Next, the Silverlight part. On the page, you need several TextBox controls for reading/writing cookies and a RadHtmlPlaceholder for displaying the HTML page that contains the cookies.



The code-behind approach is pretty simple. Execute the JavaScript methods using RadHtmlPlaceholder's IFrame.

public MainPage()
{
    InitializeComponent();
 
    // Get the IFrame from the HtmlPresenter
    HtmlElement iframe = (HtmlElement)htmlPlaceholder1.HtmlPresenter.Children[0];
    // Set an ID to the IFrame so that can be used later when calling the javascript
    iframe.SetAttribute("id", "myIFrame");
}
 
// Writes a cookie
private void SetCookie()
{
    // Define a name for the cookie
    var name = this.txtSetCookieName.Text;
     
    // Define a value for the cookie
    var value = this.txtSetCookieValue.Text;
     
    // Call the SetCookie(name,value) JavaScript method
    string code = string.Format("document.getElementById('myIFrame').contentWindow.SetCookie('{0}','{1}')", name, value);
    HtmlPage.Window.Eval(code);
}
 
// Gets a cookie
private void GetCookie()
{
    // Get the name of the cookie we're looking for
    string name = this.txtGetCookieName.Text;
     
    // Call the GetCookie(name) JavaScript method
    string code = string.Format("document.getElementById('myIFrame').contentWindow.GetCookie('{0}')", name);
    var result = HtmlPage.Window.Eval(code);
     
    // Get the value of the cookie and display it
    var value = result.ToString().Split('=')[1];
    MessageBox.Show("Value: " + value);
}
 
// Gets all cookies
private void GetAllCookies()
{
    // Call the GetAllCookies JavaScript method to retrieve all cookies
    string code = "document.getElementById('myIFrame').contentWindow.GetAllCookies()";
     
    // Display the result in a ListBox
    ScriptObject result = HtmlPage.Window.Eval(code) as ScriptObject;
    int i = 0;
    this.cookies.Clear();
    while (result.GetProperty(i) != null)
    {
        this.cookies.Add(result.GetProperty(i) as string);
        i++;
    }
}

That's it. You can take a look at the video which demonstrates the application or you can download the sample project bellow.

CookiesSupport.zip

RadControls for Silverlight


Senior Software Developer
Telerik XAML Team

1 Comment

Vuyiswa Maseko
but where is

this.cookies.Clear();

Declared ?

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by XAML Team - or - read latest articles in Developer Tools