Search code examples
c#jqueryasp.netajaxcustomvalidator

ASP.NET Custom Validator + WebMethod + jQuery + .Net 4 + does not work


my situation is like this thread :
ASP.NET Custom Validator + WebMethod + jQuery
my aspx codes :

<telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="True">
<asp:TextBox ID="TextBox" runat="server" Width="170px" ValidationGroup="A" CssClass="txt"
            TextMode="Password"></asp:TextBox>
 <asp:CustomValidator ID="cvPass" runat="server" Display="Dynamic" ControlToValidate="TextBox"
            ValidationGroup="A" ClientValidationFunction="CheckPass">
            invalid
</asp:CustomValidator>

jquery:

    function CheckPass(source, args) {
        alert('asd');
        $.ajax({
            type: "POST",
            async: false,
            timeout: 500,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "Default.aspx/CheckPassword",
            data: '{"Value":"' + args.Value + '"}',
            success: function (result) {
                alert('a');
                args.IsValid = result.d;
            }
        });
    }

code behind (c#):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Text;
using System.Net.Mail;

namespace Nasim
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod]
        [ScriptMethod]
        public static bool CheckPassword(string pass)
        {
             return false;
        }
    }
}

as you see i want to check password using custom validator in ajax mode(using jquery).
but i do n't know why $.ajax does not work for me at all.
there is no error and nothing happens and always have postback.
should i add an specific library other that jquery?
i am using visual studio 2010 + .Net 4.
what is the problem and how can i fix it?

thanks in advance


Solution

  • From the code you've showed it should work but there are potentially some other areas of your code that could be contributing to your problems.

    Some areas I'd investigate:

    • You're using a validation group for your text box and custom validator. Does the control that triggers postback also include the ValidationGroup="A" attribute?
    • When calling the Web Method you're passing in '{"Value":"' + args.Value + '"}', as your data to the method call. I think you need to change "Value" to "pass" to match the Web Method parameter name.
    • I haven't worked much with Telerik controls. Perhaps the RadScriptManager is intercepting events in some way?

    I'd recommend creating a version of your page that just uses the bare minimum of code to prove whether what you're trying to do works or not: a simple page, with a text box control, the custom validator, the jQuery code and the web method in the code behind.

    Hopefully that will help you uncover any problems.