Saturday, December 31, 2011

Happy New Year

Tuesday, December 6, 2011

How to click a button from javascrip in C#?

 Some times the __doPostBack event not working in javascript  but we need to fire an event from javascript. We all knows that its not possible to call server site event easily from javascrit so i decided to  click a button with the help of javascript function so can the related server side event will fire automatically, so we can say that from javascript we can able to call a server site event :) 

 <script type="text/javascript" language="javascript">
function ClickTheButton()
 var a = document.getElementById("hdIp");
            a.value ="Bishnu Tewary";



  <asp:HiddenField ID="hdIp" runat="server"  Value="0" />
        <asp:Button ID="myImageButton" runat="server" OnClick="myImageButton_Click" Text="Click Me"/> 

// C# Code
 protected void myImageButton_Click(object sender, EventArgs e)
            string IpAddress = Convert.ToString(hdIp.Value);

                HttpCookie ck = Request.Cookies["clientIPAdd"];
                if (ck.Value == "undefined" || string.IsNullOrEmpty(ck.Value))
                    ck.Value = IpAddress;
            catch (Exception ex) { }
            Session["IPAddress"] = IpAddress;

How to update a cookie in C#?

In my previous post we learn how to retrieve cookie in silverlight, now i am showing how to update cookies value in C# :) why C# ? hahaha because i love this :)

Here is the code for update cookie
string IpAddress="";
 HttpCookie ck = Request.Cookies["clientIPAdd"];
                if (ck.Value == "undefined" || string.IsNullOrEmpty(ck.Value))
                    ck.Value = IpAddress;

I think you remember about my post Cookie , in this post the cookie created by javascript so may be sometims its returns undefined. So when ever it returns undefined or empty value the above code will update the related (clientIPAdd) cookie value to ""

Cheers :)

How to retrieve value from a cookie in silverlight?

In my previous post I shows how to create cookie in javascript, now i am going to tell you how to retrieve value from a cookie in Silverlight. The code i'm using here is C# because i love this :)

I create a methode which returns the value from a cookie
string IPAddress = GetCookieValueByKey("clientIPAdd");
 This is the methode
 private string GetCookieValueByKey(string keyName)
            string[] cookies = HtmlPage.Document.Cookies.Split(';');
            foreach (string cookie in cookies)
                string[] keyValue = cookie.Split('=');
                if (keyValue.Length == 2)
                    if (keyValue[0].ToString() == key)
                        return keyValue[1];
            return string.empty;

Hope this helps :)

Monday, December 5, 2011

How to create cookie in javascript?

Today I am showing how to create cookie in java script. Here i am giving you an example for how to get the client IP address and save that into the cookie.

Here's the script for this
Put this script within the head tag
 <script type="text/javascript">
        var ipadd;
        function getip(data) {
            ipadd = data.ip;

//This function helping us to create the cookie
 function setvisibility() {
            var a = document.getElementById("hdIp");
            a.value = ipadd;
            var c_name = "clientIPAdd";
            var exdays = 1;
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value = escape(ipadd) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
            document.cookie = c_name + "=" + c_value;
          //Where c_name(clientIPAdd) is the  name of the and   //c_value is its value and the cookie will expire next day of creation

    <script type="text/javascript" src="">

Here is the page 

<body onload="setvisibility();">
    <form id="form1" runat="server">

        <asp:HiddenField ID="hdIp" runat="server" OnValueChanged="hdIp_ValueChanged" Value="0" />
        <asp:Button ID="myImageButton" runat="server" OnClick="myImageButton_Click" Height="1"
            Width="1" />