Saturday, December 31, 2011

Happy New Year

Tuesday, December 6, 2011

How to click a button from javascrip in asp.net 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";

document.getElementById('myImageButton').click();

 </script>

  <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);

            try
            {
                HttpCookie ck = Request.Cookies["clientIPAdd"];
                if (ck.Value == "undefined" || string.IsNullOrEmpty(ck.Value))
                {
                    ck.Value = IpAddress;
                    Response.Cookies.Set(ck);
                }
            }
            catch (Exception ex) { }
            Session["IPAddress"] = IpAddress;
            Response.Redirect("~/MyMotors.aspx");
        }

How to update a cookie in asp.net C#?

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

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

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 "127.0.0.1"

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>
    <script type="text/javascript" src="http://jsonip.appspot.com/?callback=getip">
    </script>

-------------------------------------------------------------------------------------------------------
Here is the page 


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

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

</body>