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

No comments:

Post a Comment