For calling a function from server side decorate your function with a Attribute which is as follow.
[WebMethod]
public static bool IsUserAvailable(string val)
{
if (val == "daljeet")
{
return true;
}
else
{
return false;
}
}
Now for registering your function add a script manager control to your page with EnablePageMethods property set to true.
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:Button ID="btnForPass" runat="server" Text="Click Here" OnClientClick="CallJavascript();"/>
</form>
now on the button click i am calling a javascript function which is calling the function from server side.
<script language="javascript" type="text/javascript">
function CallJavascript() {
PageMethods.IsUserAvailable(username, OnSucceeded);
}
function OnSucceeded(result, userContext, methodName) {
if (methodName == "IsUserAvailable") {
if (result == true) {
alert("Success");
}
else {
alert("Failed");
}
}
}
</script>