I've been attempting to call a simple code behind C# method using PageMethods (AJAX). I have set EnablePageMethods = true
and made my code behind method as WebMethod public static. I even tried ScriptMethod with WebMethod but nothing seems to be working. I also attempted to modify the config file without any success.
<script type="text/javascript">
function ShowAvailability() {
PageMethods.btnCheck_Click(document.getElementById("<%=txtUserName.ClientID%>").value, OnSuccess)
};
function OnSuccess(response) {
alert("On Success");
var mesg = document.getElementById("mesg");
switch (response) {
case "true":
mesg.style.color = "green";
mesg.innerHTML = "Available";
break;
case "false":
mesg.style.color = "red";
mesg.innerHTML = "Not Available";
break;
case "error":
mesg.style.color = "red";
mesg.innerHTML = "Error occured";
break;
}
}
function OnChange(txt) {
document.getElementById("mesg").innerHTML = "";
}
function test()
{
}
</script>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" EnablePageMethods="true" EnablePartialRendering="true" runat="server" />
<div>
UserName :
<asp:TextBox ID="txtUserName" runat="server"
onkeyup="OnChange(this)"></asp:TextBox>
<%--<asp:Button ID="btnCheck" runat="server" Text="Show Availability" OnClientClick="btnCheck_Click(), return false;"></asp:Button>--%>
<br />
<button id="btncheck" onclick="ShowAvailability()">Show Availability</button>
<span id="mesg"></span>
</div>
</form>