473,573 Members | 5,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validate method and causesvalidatio n

Hi,

I am implemeting a form in asp.net.
The form is quite large and the validation is reasonably complex, so I have
decieded to implement my own validation rather than use any custon
validators,

so I have a button on the form and the "causes validation" property is set
to true.
I have overridden the "Validate() " like so

public override void Validate()
{
string functionName = "Validate - page";
Trace.Warn(func tionName,"Just entered function");
Trace.Warn(func tionName,"Just exiting function");
base.Validate() ;
}

and I am checking "Page.IsVal id" in my button handler like so (please note
that it was neccessary to call the base class from the validate method in
order for me to even check the "Page.IsVal id" property.)

private void Button1_Click(o bject sender, System.EventArg s e)
{
string functionName = "Button1_Cl ick - page";
Trace.Warn(func tionName,"Just entered function");
if (Page.IsValid == true)
{
Response.Write( "Page is Valid!");
}
else
{
Response.Write( "Some of the required fields are empty");
}
Trace.Warn(func tionName,"Just exiting function");
}

however i can't find a way to set the IsValid property (nor can I seem to
override it).
The asp.net docs say this property is readonly so I guess that I can't do
this, which makes me wonder if I am implemeting my validation incorrectly.

can anybody offer some advice about validating in this method.

thanks in advance.

cheers

martin.


Nov 19 '05 #1
3 5948
you need to implement the IValidator interface, the page class uses (by
default) the BaseValidator,

your page class will need to implement the following methods

public interface IValidator
{
void Validate();
string ErrorMessage {get; set;}
bool IsValid {get; set;}
}

this is where you can set the IsValid property

then in the page class do this,

protected override void OnInit(EventArg s e) {
base.OnInit(e);
Page.Validators .Add(this);
}

protected override void OnUnload(EventA rgs e) {
if (Page != null) {
Page.Validators .Remove(this);
}
base.OnUnload(e );
}

Nov 19 '05 #2
Hi gareth,

Thanks for the tip,
I implemented I the IValidator interface

however I still can't see the IsValid property, any attempt to do so gives a
compiler error saying the property is readonly
I CAN explicitly set the private variable "isValid" to false and then
checking "Page.IsVal id" will give me false, however I can't use the propery
"IsValid" to set a value (either true or false because the compiler thinks
it is a readonly property.)

I guess that I must be doing something wrong because the interface
"IValidator " demands that i implement a "set" accessor for the property
"IsValid" -- so there must be a way to set it else why demand that it is
implemented.

I have included my either page class below so hopefully my error can be
easily pointed out to me. notice the attemp to set the IsValid property in
"Validate", which gives a compiler error, however explicitly setting the
under laying private variable does not.

cheers

martin.

public class ValidationPage2 : System.Web.UI.P age, IValidator
{
protected System.Web.UI.W ebControls.Butt on btnValidate;
private bool isValid = true;
private string errorMessage = "";

void IValidator.Vali date()
{
string functionName = "IValidator.Val idate()";
System.Web.Http Context.Current .Trace.Warn(fun ctionName,"Ente ring");
isValid = false;//OK
//IsValid = false;//Compiler error -- System.Web.UI.P age.IsValid is Read
Only
System.Web.Http Context.Current .Trace.Warn(fun ctionName,"Exit ing");
}//Validate

bool IValidator.IsVa lid
{
get { return isValid; }
set { isValid = value; }
}//IsValid

private void Page_Load(objec t sender, System.EventArg s e)
{
string functionName = "Page_Load" ;
System.Web.Http Context.Current .Trace.Warn(fun ctionName,"Ente ring");
System.Web.Http Context.Current .Trace.Warn(fun ctionName,"Exit ing");
}
public string ErrorMessage
{
get { return errorMessage; }
set { errorMessage = value; }
}//ErrorMessage

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
Page.Validators .Add(this);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.btnValidat e.Click += new
System.EventHan dler(this.btnVa lidate_Click);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void btnValidate_Cli ck(object sender, System.EventArg s e)
{
string functionName = "btnValidate_Cl ick";
System.Web.Http Context.Current .Trace.Warn(fun ctionName,"Ente ring");
if (Page.IsValid == true)
{
Response.Write( "Page is Valid!");
}
else
{
Response.Write( "Page is <b>NOT</b> Valid!");
}
System.Web.Http Context.Current .Trace.Warn(fun ctionName,"Exit ing");
}

protected override void OnUnload(EventA rgs e)
{
string functionName = "OnUnload";
System.Web.Http Context.Current .Trace.Warn(fun ctionName,"Ente ring");
System.Web.Http Context.Current .Trace.Warn(fun ctionName,"Exit ing");
if (Page != null)
{
Page.Validators .Remove(this);
}
base.OnUnload(e );
}

}


Nov 19 '05 #3
Sorry for jumping in so late. I am the author of a replacement to
Microsoft's validators (Professional Validation And More). So I have a lot
of knowledge of ASP.NET validation.

This approach is not a good one. ASP.NET validation wants individual
webcontrols to handle validation. Either they are subclassed from
BaseValidator, created using a CustomValidator , or created by making a
WebControl that implements the IValidate interface. You are trying to
implement this directly into the page, which you should not think of as a
webcontrol (even though its subclassed from System.Web.UI.C ontrol). The Page
object must have each control that implements IValidate in its
Page.Validators collection and it expects controls, not itself, in that
list.

You haven't said why its complex enough to abandon using CustomValidator s. A
CustomValidator is very easy to use. In fact, you probably have built all
the logic into the overridden Validate() method. All you need to do is move
that logic into the CustomValidator 's own evaluation method (as documented
in the .net docs).

One thing a validator supplies is an error message on the page in the
location it appears on the page. You will not get this behavior by using
Response.Write. In fact, Response.Write( ) called anytime before the Render
phase of the page will be output before the first HTML tag of the page! So
it will be outside the <html> tag and appear at the very very top.
If you want to position text on the page, add a Label control in the right
location. To show or hide it, change its Visible property.

Validators are actually subclassed from Label! They are really a Label that
has intelligence to show and hide itself.

All of these are reasons why you should be using CustomValidator s and not
trying to go in another direction.

A little plug for my software (http://www.peterblum.com/vam/home.aspx). I
had built around 10 validators using Microsoft's validator classes a few
years back and kept running into limitations that left my customers wanting
more. I needed to rewrite their framework to resolve these problems. (A list
of the limitations I've found: http://www.peterblum.com/vam/valmain.aspx)
When you use Professional Validation And More's 25 validators, you will
rarely write any custom code. I've also expanded what validation offers,
such as setting focus to the field with the error, changing that field's
color, showing an image instead of an error message (where the image pops up
the error message), and a ValidationSumma ry that has hyperlinks on each
error message to set focus to the field associated with that error message.

--- 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

"Martin" <ma************ @martinz.co.nz> wrote in message
news:OL******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

I am implemeting a form in asp.net.
The form is quite large and the validation is reasonably complex, so I
have
decieded to implement my own validation rather than use any custon
validators,

so I have a button on the form and the "causes validation" property is set
to true.
I have overridden the "Validate() " like so

public override void Validate()
{
string functionName = "Validate - page";
Trace.Warn(func tionName,"Just entered function");
Trace.Warn(func tionName,"Just exiting function");
base.Validate() ;
}

and I am checking "Page.IsVal id" in my button handler like so (please note
that it was neccessary to call the base class from the validate method in
order for me to even check the "Page.IsVal id" property.)

private void Button1_Click(o bject sender, System.EventArg s e)
{
string functionName = "Button1_Cl ick - page";
Trace.Warn(func tionName,"Just entered function");
if (Page.IsValid == true)
{
Response.Write( "Page is Valid!");
}
else
{
Response.Write( "Some of the required fields are empty");
}
Trace.Warn(func tionName,"Just exiting function");
}

however i can't find a way to set the IsValid property (nor can I seem to
override it).
The asp.net docs say this property is readonly so I guess that I can't do
this, which makes me wonder if I am implemeting my validation incorrectly.

can anybody offer some advice about validating in this method.

thanks in advance.

cheers

martin.

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1621
by: Paul | last post by:
Hi, I hope somebody can help me with the following. (I have tried searching for a solution on the newsgroups but nothing is quite what I need. Feel free to point me in the direction of anything I may have missed though.) I have a form with 2 text boxes, two list boxes and 2 buttons (A & B). Both buttons must perform some validation but...
5
2523
by: Jim Heavey | last post by:
When should you use the Page.Validate() method? I thought you would use this method if you have some Server side validation (CustomControl's) you wanted to use and this would cause them to be invoked. I am probably wrong about that. If I am suppose to use this function, the edits seemed to be invoked even when you have pressed the cancel and...
11
2113
by: wolf | last post by:
There are three webcontrol on my asp.net form: a TextBox, a submit button and a RegularExpressionValidator. And I had set ControlToValidate property of the RegularExpressionValidator to the TextBox. But now, when I input a string in the textbox and press Enter key, the form was submit without client javascript validate. Why? And what should...
8
5391
by: tshad | last post by:
I get the following message from one of my buttons where I have CauseValidation=false. Page.IsValid cannot be called before validation has taken place In my routine I need to do: if Not IsValid then exit sub end if
4
1450
by: msnews.microsoft.com | last post by:
I have 2 datagrids. Each with an add record in the footer. How can I just validate the fields in that datagrid without validating the fields in the 2nd datagrid. Any examples??? -- David
7
1375
by: Rich | last post by:
Hello, I have 10 textboxes on a form. I would like to validate text-data entry on each textbox. I was looking for a Form_Current event or Form_Change event but did not recognize any such events. Or maybe there is a property for the Textboxes where I can set data entry conditions? Wondering about CausesValidation textbox property-how to...
9
6656
by: B-Dog | last post by:
I have a form that has about 10 text boxes on it, they all have to be filled out before submitting is there a quick way to make sure that none are null or do I have to call out each textbox? Say something like textbox1 through textbox10? Thanks
1
2624
by: Joseph | last post by:
I have simple app with a textBox, a button and errorProvider. I have all the controls CausesValidation set to true, and the following code: private void textBox1_Validation(object sender, CancelEventArgs e) { string error = null; if( textBox1.Text.Length == 0 ) { error = "Please enter a name"; e.Cancel = true;
11
11747
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider in the validated event. Is there a way to know if all validated controls pass validation when the user clicks an OK button? In ASP.Net there's...
0
7709
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
8039
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8218
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
7800
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...
1
5605
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
3738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3744
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.