0

I have a button in ASP.NET. When the user clicks the button, I want to make some validation beforehand, so the postback only occurs if the validation method returns true.

My thought was to use the logic of the OnClientClick of the button. If you say OnClientClick="return false;", it won't give a postback, while returning true will. However, that's not working, as the resulting behaviour is it doesn't make a postback even though what my jQuery method returns.

Currently I have:

Markup:

<asp:Button runat="server" ID="ReplyBtn" OnClientClick="return ValidateInput();" OnClick="ReplyBtn_Click" CssClass="btn-large btn-success"
    Text="Answer" />

JavaScript:

function ValidateInput() {
        var value = stripHtml(CKEDITOR.instances['ReplyBox'].getData()).trim();
        var options = {
            type: "POST",
            url: "../../Services/ForumOperationService.svc/ReplyToPostFeedback",
            data: '{"content":"' + value + '"}',
            async: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                $("#StatusReplyLbl").text(msg.ReplyToPostFeedbackResult);

                if (msg.ReplyToPostFeedbackResult) {
                    return false;
                } else {
                    return true;
                }

            },
            error: function () {
                return true;
            }
        };
        $.ajax(options);
        return false;
    }

    function stripHtml(str) {
        return $('<div />', { html: str }).text();
    }

So basically, what's wrong with this code? And how can I perform validation on my button before making a postback?

3 Answers 3

4

Your function unconditionally returns false, since you aren't using the response from the ajax object. For example, let us assume your server side script returns the text "OK" if your validation succeeds. You could try something like this:

function ValidateInput() {
    var value = stripHtml(CKEDITOR.instances['ReplyBox'].getData()).trim();
    var options = {
        type: "POST",
        url: "../../Services/ForumOperationService.svc/ReplyToPostFeedback",
        data: '{"content":"' + value + '"}',
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $("#StatusReplyLbl").text(msg.ReplyToPostFeedbackResult);
        }
    };
    var resp = $.ajax(options);
    return (resp.status==200 && resp.responseText == "OK");
}

Ideally, what you would do is unconditionally return false, and trigger a postback yourself in the success handler.

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

1 Comment

Thanks - that solution worked and will work short term.. :-) We will definetely consider triggering a postback instead . Thanks again.
1

Not 100% on what you're asking, but I believe the problem is that the function is always returning false even when you expect it to return true from the ajax request?

As mentioned above, ajax running async would cause this not to work but it looks like you're setting async to false.

The issue is your returns.

return true; inside the ajax function returns out of the success function of the ajax call, not the overall function.

I don't think this is the best way to achieve what you're going for, but based on the code you already have, here is what I think you need.

function ValidateInput() {
    var replayToPost = false;
    var value = stripHtml(CKEDITOR.instances['ReplyBox'].getData()).trim();
    var options = {
        type: "POST",
        url: "../../Services/ForumOperationService.svc/ReplyToPostFeedback",
        data: '{"content":"' + value + '"}',
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $("#StatusReplyLbl").text(msg.ReplyToPostFeedbackResult);

            if (msg.ReplyToPostFeedbackResult) {
                replayToPost = false;
            } else {
                replayToPost = true;
            }

        },
        error: function () {
            return true;
        }
    };
    $.ajax(options);
    return replyToPost;
}

Comments

1

The return result in your ajax function will be a return from the success or error methods therefore it will not bullble up to the ValidateInput() method. You will need to declare a variable outside of the scope of your ajax call and then return that result.

function ValidateInput() {
    var value = stripHtml(CKEDITOR.instances['ReplyBox'].getData()).trim();
    var result = false;

    $.post("../../Services/ForumOperationService.svc/ReplyToPostFeedback", '{"content":"' + value + '"}', function (msg) {
            $("#StatusReplyLbl").text(msg.ReplyToPostFeedbackResult);
            if (!msg.ReplyToPostFeedbackResult){
                result = true;
            }
    });

    return result;
}

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.