UserControl sometimes skips validation

E

Eric Maia

I have two UserControls I am using in a form. These are each also used
separately in two other forms. The structure is essentially this:

CourseUserControl.ascx - select or enter a course
SessionUserControl.ascx - select or enter a course session

CourseDetails.aspx - a form which contains just CourseUserControl
CourseSessionDetails.aspx - a form which contains just
SessionUserControl
TrainingRequestDetails.aspx - a form which includes both controls,
plus additional fields.

Each UserControl has an entry form with its own validation. When the
UserControl's submit button is clicked (CausesValidation=true), the
event handler in the UserControl's cs file checks the IsValid
properties just of the validators that belong to the control (not
Page.IsValid).

This works fine on the pages that have a single UserControl - the
validators are checked, and any validation messages are displayed
properly.

When I try using the SessionUserControl to enter a new Session record
from the page that has both controls, the validators in the
UserControl are not called properly. The button event handler fires,
but the validators are always valid.

I even added code to specifically call each validator's Validate()
method directly in the event handler before I check whether the
pertinent validators are valid. It doesn't make any difference - the
validators always show IsValid=true when the control is called from
the page that has both controls.

Does anyone know why a change in the containing page would affect
validation behavior when the validation is encapsulated within the
UserControl?

Thanks - Eric
 
P

Peter Blum

Are you loading the UserControls programmatically by using
Page.LoadControl()?

This can change the UniqueID properties of the various controls. The
UniqueID reflects the containers of its parent's. A usercontrol is a
container. Suppose you have a textbox on a UserControl whose ID is "One" on
the first pass. Then when posting back, you recreate the UserControl and
assign the ID of "Two". The textbox will have "One_:TextBox1" first and
"Two_:TextBox1" second. Often users don't assign the ID property on a
UserControl after its loaded. This lets ASP.NET assign it for you. If the
page is not recreated with an identical list of controls, ASP.NET may
generate a different value for the UserControl's ID property.

So your goal is to assign a specific ID to any UserControls you load
programmatically.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
E

Eric Maia

Thanks, Peter. However, I am not adding the UserControls
programmatically - they are declared as protected members at the top of
the containing page's code-behind.

The event-handling code and validation code is also all encapsulated
within the UserControl.

Here's a snippet of code from the UserControl's event handler (the
button has CausesValidation="true")...

private void CourseEntryButton_Click(object sender, System.EventArgs e)
{
// need to programmatically check just the
// relevant validation controls rather than Page.IsValid...
if (this.CourseTitleRequiredValidator.IsValid
&& this.CourseUniqueValidator.IsValid
&& this.VendorCustomValidator.IsValid
&& this.NewVendorNameUniqueValidator.IsValid
&& this.SubcategoryCustomValidator.IsValid
&& this.CEUSCustomValidator.IsValid)
{
... calls DB update method, updates the control's display, and fires a
custom event up to the containing page...
}

This works fine when the control is by itself in the CourseDetails form.
However, on the page with both controls, it breezes right by the if
statement and jumps to the DB update method, which then chokes if the
input is invalid.

I even changed the validation event handlers to comment out everything
and leave just args.IsValid=false; and they still evaluate to true.

As I understand the docs in the Framework Developer's Guide, they say to
leave all event handling to the UserControl, and not try to handle its
events from the containing page. Is this wrong? Is the containing page
somehow messing up the validators' delegation?

Thanks - Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top