开发者

Validation problems

So I have an ASP.NET/VB web application in which I'm using both RequiredFieldValidators and CustomValidators. However, I have been having some major issues with all the validation. When you click a "Next" button to go to the next page of the form without filling in the required fields, all the RequiredFieldValidators fire and the error messages show up, but after a moment it still goes to the next page. Sometimes after changing what seem to be unrelated parts of the code, it starts working again, and works for awhile until I change something else. The CustomValidators are even more inconsistent, sometimes working, sometimes firing but not preventing the user from moving to the next page, and sometimes not working at all, but only stop/start working when I change code that seems like it shouldn't be effecting it. Sometimes one works an开发者_如何学Pythond one doesn't. What's driving me crazy about this is it seems so random. And I can't seem to pinpoint what changes in the code actually effect this. Does anyone have any ideas? Is there anything that would effect all the validation on a page at once? Here is some of my code, I can also post some of the code behind if necessary.

Required Field Validator:

<asp:RequiredFieldValidator ID="rfvFirstName" ControlToValidate="txtFirstName" runat="server"SetFocusOnError="true" ErrorMessage="Required"></asp:RequiredFieldValidator>

Custom Validators:

<asp:CustomValidator ID="cvRequired" runat="server" ControlToValidate="txtCourseNum" Enabled="true" ErrorMessage=""></asp:CustomValidator>
<asp:CustomValidator ID="cvDuplicate" runat="server" ControlToValidate="txtCoursePrefix" Enabled="true" ErrorMessage=""></asp:CustomValidator>

Next button:

<asp:Button ID="btnNextA" runat="server" Text="Next" OnClick="btnNext_Click" UseSubmitBehavior="true" CausesValidation="true" />


Sara, make sure the buttons that shouldn't be validating have the CausesValidation set to false. Also if you end up posting back for validation, make sure to look at the Page.IsValid property before assuming things are valid. Validators will fire before postback events. Is your next button posting back-- if so, then check for that.

I bet if you load the page for the first time and don't put in a value for the required field, then you will be prevented from going to the next page. But if you have a value for the required value, you will see validators fire, but you will go to the next page. The code behind for the next button should check for the Page.IsValid property.

The reason the validation is confusing is that some validators will by default occur client side (required field validators for instance) while some will occur server side (like custom validators that have server side methods subscribed to their events). If a client side validator fails, you'll never make it to the server. Also, if the client side validators pass your server side validators will run, but.... and this is the tricky part, your post back handler will fire after the validator fires. So if you redirect in your post back handler (let's say the post back was caused by a button click) then even if your server side validator fails, you will be redirected so your validator appears not to be working. It probably worked, but you might not have checked the Page.IsValid property.

Again it's important to note that the order is:

  • client side validation,
  • server side validation,
  • then your post back event

I wouldn't swear on a stack of bibles (because I don't have a stack of bibles), but what you see as inconsistent behavior could very well be that you are not actually performing things in the same order. If you put nothing in the required fields, then you should never be posting back. If you put something in the required fields and one of your server side validators fails, then you'll post back, but if you don't test for Page.IsValid it will appear as if validator is not working. event handlers.


First of all, put validation group on validators and the button. If that too doesnt work, put OnClientClick='CheckValidate();' and declare the function which will call page_clientvalidate method along with the parameter.. validation group. This would surely work. If that is not working put debugger in the javascript method and debug the same

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜