views:

30

answers:

2

Hey,

I'm using asp:requiredfieldvalidator on my page. The usual way of turning this off for a control is to set

CausesValidation="false"

However I've got more buttons that I don't want to cause validation than I have that do want validation.

So my thinking is that it would be easier to turn it off by default and on when I need it but I don't know how.

Thanks in advance

A: 

I don't think you can turn it off by default, but you could set ValidationGroup property on the buttons you want to cause validation and the controls they should validate.

A code sample:

<asp:TextBox ID="tb1" runat="server" ValidationGroup="ValidateMe" />
<asp:TextBox ID="tb2" runat="server" />
<asp:RequiredFieldValidator" ID="rfv1" runat="server" ControlToValidate="tb1" ValidationGroup="ValidateMe" />
...
<asp:Button ID="btnSubmit" runat="server" ValidationGroup="ValidateMe" />

When btnSubmit is clicked, only tb1 will be validated, regardless of any other validation controls applied to tb2.

A S
Not quiet what I was hoping, but its given me some ideas
mjmcloug
A: 

In your markup you can set the Enabled flag (which I would have said is the more definite way to turn it off than with CausesValidation):

<asp:requiredfieldvalidator Enabled="false" id="rfv1" ControlToValidate="whatever" />

Then selectively enable it in your code-behind with:

rfv1.Enabled = True

CausesValidation comes in when you want to click a button while the validator is enabled. <asp:button id="btnSubmit" Text="Submit" /> won't work while the validator is enabled, but <asp:button id="UnvalidatedButton" Text="Toggle Validation State" CausesValidation="false" /> can still be clicked regardless of the state of the validator.

PhilPursglove