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?

Sparky
  • 98,165
  • 25
  • 199
  • 285
RePierre
  • 9,358
  • 2
  • 20
  • 37
  • you can go through this link http://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text – शेखर Jan 30 '14 at 08: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)? – RePierre Jan 30 '14 at 09:13
  • have you tried console log or alert the value `jsonResult.CanDelete` what you are getting there? – शेखर Jan 30 '14 at 09: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. – RePierre Jan 30 '14 at 09: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. – शेखर Jan 30 '14 at 09:46

2 Answers2

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 ,

Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
  • 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;`. – RePierre Jan 30 '14 at 09:24
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;
});

}
RePierre
  • 9,358
  • 2
  • 20
  • 37