473,382 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

UserControl sometimes skips validation

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
2 2221
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: KB | last post by:
Hi guys, How to cause validation inside the UserControl without validating the whole form. I have controls that must be validated on both the form and in the UserControl. But in the UserControl...
1
by: Simon | last post by:
Hi, Has anyone experienced the problem where validation controls stop a previously working page from submitting. Sometimes, when I move between my work machine and my home machine validation...
1
by: freshRecruit | last post by:
Hi, I am having a problem, and is driving me nuts and my deadline is fast approaching. Please do help me.. This is a webapplication with a usercontrol which has some buttons for adding,...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
2
by: sonu | last post by:
Hi all, I have a problem regarding use of a usercontrol in .NET. My usercontrol consists of two listviews which I say as source and destination lisviews which contains the files and folders. I...
0
by: Arpan | last post by:
I have created the following UserControl (the file is named LoginForm.ascx): <script language=VB runat="server"> Public UName As String Public UPwd As String Public BColor As String Public...
14
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of that control on a Page ("Defaul.aspx"). The control works fine. Now, I want to be able to use "FindControl()" from...
0
by: Nariban Barkan | last post by:
Hi All, I have two UserControls, KK1 and EFT1. Each one has one ScriptManagerProxy and one UpdatePanel . in addition to these, on first control (KK1) there is one button. on second control...
3
by: COHENMARVIN | last post by:
I have an aspx page that loads a usercontrol. Can that usercontrol load another usercontrol into part of it? Thanks, Marv
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.