473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

CourseUserContr ol.ascx - select or enter a course
SessionUserCont rol.ascx - select or enter a course session

CourseDetails.a spx - a form which contains just CourseUserContr ol
CourseSessionDe tails.aspx - a form which contains just
SessionUserCont rol
TrainingRequest Details.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 (CausesValidati on=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 SessionUserCont rol 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 2237
Are you loading the UserControls programmaticall y by using
Page.LoadContro l()?

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_:TextB ox1" first and
"Two_:TextB ox1" 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
programmaticall y.

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlu m.com
Creator of "Profession al Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Eric Maia" <er*******@metr okc.gov> wrote in message
news:e7******** *************** **@posting.goog le.com...
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:

CourseUserContr ol.ascx - select or enter a course
SessionUserCont rol.ascx - select or enter a course session

CourseDetails.a spx - a form which contains just CourseUserContr ol
CourseSessionDe tails.aspx - a form which contains just
SessionUserCont rol
TrainingRequest Details.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 (CausesValidati on=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 SessionUserCont rol 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
programmaticall y - 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 CausesValidatio n="true")...

private void CourseEntryButt on_Click(object sender, System.EventArg s e)
{
// need to programmaticall y check just the
// relevant validation controls rather than Page.IsValid...
if (this.CourseTit leRequiredValid ator.IsValid
&& this.CourseUniq ueValidator.IsV alid
&& this.VendorCust omValidator.IsV alid
&& this.NewVendorN ameUniqueValida tor.IsValid
&& this.Subcategor yCustomValidato r.IsValid
&& this.CEUSCustom Validator.IsVal id)
{
... 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=fa lse; 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
1054
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 it's separate from the form. Thanks in advance
1
1440
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 that was previously working perfectly just starts to bugger up the page. One normally happens is I'll have it working fine at work and then when I
1
3819
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, deleting and updating to a database. When the user clicks the "Add" button than the information provided is validated against the database. If the...
0
2458
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 which are not behaving as I would expect. Specifically, if there is a command button external to the usercontrol which is activated by a shortcut...
2
1383
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 select files from source view and transfer it to the destination view.The problem arises while displaying the dialog for overwriting the file or...
0
1134
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 Sub btnSubmit(ByVal obj As Object, ByVal ea As EventArgs) lblMessage.Text = "UserName : " & uid.Text & "<br>Password : "
14
3187
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 within my Default.aspx as follows: MyUC C = (MyUC)this.FindControl("SomeID"); When I try to run this, I get error: "The type or namespace name...
0
1018
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 (EFT1), there are several textboxes , RequiredFieldValidators for textboxes and one button. I load these usercontrols via the PlaceHolders, like...
3
1469
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
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.