views:

34

answers:

2

I have a few controls on a page that are all in the same validation group. Depending on certain conditions, one of these controls can be set to visible=false (and the user wont be able to input anything into it). If this happens, is there a way to remove said control from the validation group? Code like this:

if(testControl.Visible==false) testControl.ValidationGroup="";

does nothing. Yet, if I remove the validationgroup from the aspx page like so:

<asp:RequiredFieldValidator ID="testControl" runat="server" validationgroup=""></asp:RequiredFieldValidator> 

The page will validate. Is there a way around this?

A: 

Are you sure your code is being hit in the code-behind file? I mean, your control is really invisible when you check that condition?

if(testControl.Visible == false)
   testControl.ValidationGroup = string.Empty;

Put a breakpoint in testControl.ValidationGroup=""; and see if the debugger stops there.

Where is the code above? It should be inside the PageLoad method for example.

Call Page.Validate("NameOfYourValidationGroup") after that code.

What is the problem here I think:

You're setting this testControl with Visible = False and then you post back to the server. When you do testControl.ValidationGroup = string.Empty it'll have no effect because it has already posted back to the server:

From MSDN:

TextBox..::.ValidationGroup Property

Gets or sets the group of controls for which the TextBox control causes validation when it posts back to the server.

This way you should call this code testControl.ValidationGroup = string.Empty; when you hide your control setting it to Visible = false so that when the page loads again for the user the control won't be assigned to that ValidationGroup. Now, if you postback the page it should validate the way you want it.

Set testControl.CausesValidation = false too.

Leniel Macaferi
Tried it. The debugger does go over testControl.ValidationGroup=""; hence my confusion
Jbravo42
At the moment, a random function. However I have tried it in both init and page load....no success
Jbravo42
Are you sure you're not assigning a validation group to that control at a later time in the code?
Leniel Macaferi
Its not assigned anywhere else (at least that I can find). Going to try prerender now
Jbravo42
Just to be sure: when you put that code in the PageLoad method the debugger stops there? Your control is Visible == false?
Leniel Macaferi
Prerender didn't work either :(Yes it did stop there and was visible==false
Jbravo42
Remember that you should put that code in the PageLoad method before the if (Page.IsValid) assertion...
Leniel Macaferi
Definitely did that. At this point I think it might be better to write my own validation code if the control is hidden. Thank you for your help though, it is appreciated :)
Jbravo42
Try to call the Validate method for that group after that code. http://msdn.microsoft.com/en-us/library/system.web.ui.page.validate%28VS.90%29.aspx
Leniel Macaferi
The Page.IsValid still fails
Jbravo42
Still didn't work (compile error). I dont know if its good form to mark an answer as complete when its not, but if you want points for it its yours.
Jbravo42
No... I don't mind. I'd like to help you get this thing working.
Leniel Macaferi
Heh, me too. Writing my own validation seems to have worked, but if we can get it to work the proper way, all the better
Jbravo42
Did you understand what I wrote in my answer? When you check for Page.IsValid it won't consider `testControl.ValidationGroup = string.Empty;` because the validation was done when the page was posted back to the server. At that time the control still was part of that ValidationGroup.
Leniel Macaferi
yeah that makes sense, but it still didn't work Guess I am going to have to stick with if(Page.IsValid || myOwnSetOfConditions)
Jbravo42
testControl.CausesValidation is already false
Jbravo42
When do you set testControl.Visible = false?
Leniel Macaferi
On the properties window when viewing the aspx code
Jbravo42
Shouldn't you set it to Visible = false depending on some condition as you describe in your question? Shouldn't it be during runtime?
Leniel Macaferi
whoops. Misread that as testControl.causesValidation.Visible is set as false in a helper function. The Order is, Init, helperFunction, Page_Load
Jbravo42
Try to put this code `testControl.ValidationGroup = string.Empty;` in that helper method and test it again.
Leniel Macaferi
nope, still nothing
Jbravo42
A: 

Does the following help?:

testControl.IsValid = true;

Use for ex. with:

Page.Validate();
// manual override of specific control
testControl.IsValid = true;

// Guard
if (! Page.IsValid) return; // Or do own custom logic
// else
// Do your stuff here...
JonW