0

I have a ListView whose template has a LinkButton with a CustomValidator.

<ItemTemplate>
    <div>
        <asp:LinkButton runat="server" ID="_linkButtonDelete" 
            ValidationGroup='<%# DataBinder.Eval(Container.DataItem. "Id") %>'
            CausesValidation="true" />
        <asp:CustomValidator runat="server" ClientValidationFunction="validateDelete"
            ValidationGroup='<%# DataBinder.Eval(Container.DataItem. "Id") %>'
            data-itemId='<%# DataBinder.Eval(Container.DataItem. "Id") %>'>*</asp:CustomValidator>
    </div>
</ItemTemplate>

In validateDelete function I perform a synchronous AJAX request to determine whether the specific item can be deleted.

function validateDelete(sender, args){
    var itemId = sender.dataset.itemid;
    $.ajax({
        async:false
        // other settings omitted
        success: function(jsonResult){
            args.IsValid = jsonResult.CanDelete;
        }
    });
}

However, when I click on a button for which validateDelete function sets args.IsValid = true (I checked the response with Fiddler and by debugging the function) the link does not trigger a postback and the validator is invalid (i.e. I can see the red * near the button).

Why does the validator remain invalid?

5
  • you can go through this link stackoverflow.com/questions/1225667/… Commented Jan 30, 2014 at 8:49
  • @Șhȇkhaṝ, thank you for your comment but my problem isn't getting data from AJAX call - it's why does the validator remain invalid (even if the ajax call is blocking the method until receiving a result)? Commented Jan 30, 2014 at 9:13
  • have you tried console log or alert the value jsonResult.CanDelete what you are getting there? Commented Jan 30, 2014 at 9:31
  • I did not log the value but I added a bit of code to show/hide a message in page depending of what the value of args.IsValid is and the message is shown when args.IsValid = false; and hidden when args.IsValid = true;. This side-effect shows that I am setting correct values for IsValid property. Commented Jan 30, 2014 at 9:40
  • just put alert before args.IsValid = jsonResult.CanDelete; line and see does it work or not. What I am thinking is it is not waiting for the ajax result function. Commented Jan 30, 2014 at 9:46

2 Answers 2

1

i implement your scenario, and cause i do not know your code behind, i sent my request to a ashx handler :

$.ajax({
    async: false,
    url: "Handler1.ashx",
    success: function (jsonResult) {
        args.IsValid = jsonResult == "False" ? false : true;
    }
});

and this is handler1.ashx implementation :

public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    context.Response.Write(true); // this return value was changing manually
                                  // to test both true and false situations
    context.Response.End();
}

public bool IsReusable
{
    get
    {
        return false;
    }
}
}

everything work fine, probably the problem is where you assign args.IsValid, try to cast jsonResult.CanDelete if its not boolean before set args.IsValid, like something i have done using iif, may your problem be solved... i do not know, whether this javascript codes you copy here is differ with its original on your page... but after async:false u need a ,

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer! However, the problem is not in the validation function. I put a breakpoint in it and added several side-effects to make sure the args.IsValid is indeed true. However, validator is still invalid after setting args.IsValid = true;.
1

Thanks to hints from @Șhȇkhaṝ and @am1r_5h and the suggests from here, namely

setting args.IsValid at the end of the code

I was able to perform validation on client by refactoring the validateDelete function into this:

function validateDelete(sender, args){
    var itemId = sender.dataset.itemid;
    var isValid; // <- declare outer scope variable to hold the result
    $.ajax({
        async:false
        // other settings omitted
        success: function(jsonResult){
        isValid = jsonResult.CanDelete; // <- set the result of ajax call
    }
    // Set args.IsValid at the end of the function.
    args.IsValid = isValid;
});

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.