Custom validator fires but does not prevent postback

断了今生、忘了曾经 提交于 2019-12-05 08:13:14

Everything looks ok althought I am not sure why you are attaching the Click function to your submit button. I would remove that and test it as it maybe be overriding the default behavior.

Also I think you need to capitalize the IsValid property:

args.IsValid = false;
Shalini

I too faced this issue, I was trying to add a custom validator to a dropdownlist which had a selectedIndexChange event attached to it. After i gave incorrect value for dropdown, i was able to se ethe error message i gave in Custom Validator but immediately after it Postback was happening.

However on adding this property CausesValidation="true" to the dropdownlist control resolved my issue.

Postback wasn't happening on incorrect value after adding this property to my dropdown.

If it helps other people, I had a Validation group that I forgot to add the button to.

Make sure to add the button, the textbox and the validator to the same validation group for the postback to be prevented.

Richard

I experienced this problem as well.

What I did was, in the C# procedure that was called by the button, at the top I added

if (IsValid == false)
         return;

I could not stop it performing the postback so this seemed to me like the only solution.

andy_b

You are misssing ControlToValidate="txtWhyUnlikely"

Posting this as it might help someone that is getting the same weird behavior.

Initially I had the same issue as this post title. I checked all the suggestions here but my code seemed to be fine.

To fix this I replaced my cause validation control <asp:Button.. with a <button.. . Not sure why this is happening but happy it's working now.

hth

<button tags are missing the correct javascript code to validate. <asp:Button does have the correct javascript rendered.

I've added this to any button tags:

btn.Attributes("onclick") = StringFmt("if(!Page_ClientValidate(''))return false;")

and that solved the post-back issue. No post-back occurs if the client-side detects an issue.

I solved this problem by creating a variable:

 Boolean fieldIsValid = true;

and at the custom validating expression I would change the value if arguments weren't true:

if(args.IsValid == false)
            {
                fieldIsValid = false;
            }
            else
            {
                fieldIsValid = true;
            }

Then, I also put that in the submit click method:

protected void submit_Click(object sender, EventArgs e)
        {
            if (fieldIsValid)
            {
                //submit my things
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!