Call Server Side Function From Javascript


In design :
In the head section:

 <script type="text/javascript" language="javascript">
    function callme(chkCall)
    {
       var chkCall=document.getElementById(chkCall);
       if(chkCall.checked==true)
        {
             chkCall.checked=true;
             PageMethods.GetContactName();
        }
        else
        {
              chkCall.checked=false;
        }
    }
    </script>

In the body section:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<asp:CheckBox ID="chkCall" runat="server" />

In the CS part :

protected void Page_Load(object sender, EventArgs e)
    {
        chkCall.Attributes.Add("onclick", "javascript:callme('" + chkCall.ClientID + "')");
    }

    [System.Web.Services.WebMethod]
    public static void GetContactName()
    {
        Response.Write("your name");
    }

Comments