开发者

Field validation of a single button only

I have the following validator on a textbox inside a modal dialog box.

<asp:RequiredFieldValidator runat = "server" 
                            ErrorMessage = "Role name can not be empty."
                            ControlToValidate = "tbxRoleName" />

It works like it should, except that the validation triggers on every other buttons OnClick handler on the aspx-page too. Since the dialog is invisible it looks like buttons just dont work on the page. My workaround is to add CausesValidation = "false" on all bu开发者_如何学Gottons on the page. But it is a very bad solution and I think there should be a smarter way.


Assign ValidationGroup to each validator and also to the button that should trigger validation (but not the the other button). Something like:

<asp:RequiredFieldValidator ValidationGroup='valGroup1' ... />

<asp:Button ValidationGroup='valGroup1' Text='I trigger validation' ... />


How about setting a ValidationGroup?

http://msdn.microsoft.com/en-us/library/ms227424.aspx


Also you can use 'causesvalidation' to the button. If it is false Button will not response to Validation in aspx page.

Example: <asp:Button runat="server" Text="Cancel" CausesValidation="false" />


The Button has a property CausesValidation that can disable the validation for that button. More info here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜