Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Setting CausesValidation in a UserControl

Try this out:

Create a UserControl. Drag a button onto it. Set the button's CausesValidation property = False.

Create a Form. Drag the UserControl you just created onto this Form.

Build this form as a dll.

Create another Form. Inherited from the first form. Drag a textbox onto this inherited form.

Put a breakpoint in the textbox's validating event handler.

Run

Click on the textbox, then click on the button. Why does the textboxes validating event fire? It should not as the button's CausesValidation property is set to False.

It looks like the UserControls child controls' .CausesValidation properties are ignored & whatever's set at the UserControl level controls is used.

9 Comments

  • There's some serious bugs with [Custom|NonCustom] Controls within Custom Controls with ASP.NET. I had to completely abandon one control I was developing because properties and events within child controls were being completely ignored. Real shame. It seems that the only thing that worked for me was to keep it at a n := 2 level for child controls. Anything else seemed to not function properly.

  • i think that the validation runs because you "leave the textbox", not because you click the button. maybe try a simpler test?



    cheers

    lb

  • I haven't tried that same scenario, but to me it sounds like it works like it is supposed to. From the perspective of the form, the user control is a single control member. The user control represents an encapsulation boundary - a component. The consumer of that component should not be expected to know or care about any of the implementation details of that component, such as what contained controls think about validation at the form level. If you want to expose that, you need to manage it through the CausesValidation property on the user control - the portion of its public interface that is exposed to clients.



    I am missing something or talking babble here?

  • Brian, what you state is correct. We built a CommandManagement type that works with a UserControl that has 5 buttons in it: Cancel, Add, Edit, Delete, Save. We have been using this UserControl all over the place. We recently discovered that setting CausesValidation to False on the Cancel and Delete button does not work. So, we tried to set it at the UserControl level. This causes all buttons, as expected, to 'block' the previous controls Validating event. We need the Validating event to fire when you leave a control and click on Save, Add or Edit.

  • in what scenario would a person ever do that thing. You're just so trying hard to pin point bugs. It doesn't make any sense.. You're just a PRO WANNABE :)

  • George: Your inexperience is showing:

    I have a customized delete button that inherits from ButtonField that I use in a GridView control. Problem is, the GridView isn't the main form, but I need it on the page for functionality (kind of a subform, if you will). Even though I specifically set CausesValidation=False, and EVEN though I have specified that the other validator controls are part of a completely separate ValidationGroup; when I click the custom delete button it fires off every validator on the page.

    That is a bug, and I have found no answers anywhere to indicate differently. Because my button class inherits from the ButtonField class, the CausesValidation property should function correctly, but it doens't.

  • Увлекательно. Поброжу у вас еще. А долго ли писали этот пост?

  • не согласен с админом. стукни в асю плиз 606139

  • Maybe Alcazzarre could help you to solve your problems? You'r welcome pal.

Comments have been disabled for this content.