By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,705 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,705 IT Pros & Developers. It's quick & easy.

UserControl sometimes skips validation

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Eric Maia" <er*******@metrokc.gov> wrote in message
news:e7*************************@posting.google.co m...
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

Nov 18 '05 #2

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.