validate method and causesvalidation

M

Martin

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(functionName,"Just entered function");
Trace.Warn(functionName,"Just exiting function");
base.Validate();
}

and I am checking "Page.IsValid" 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.IsValid" property.)

private void Button1_Click(object sender, System.EventArgs e)
{
string functionName = "Button1_Click - page";
Trace.Warn(functionName,"Just entered function");
if (Page.IsValid == true)
{
Response.Write("Page is Valid!");
}
else
{
Response.Write("Some of the required fields are empty");
}
Trace.Warn(functionName,"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.
 
G

garethdjames

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(EventArgs e) {
base.OnInit(e);
Page.Validators.Add(this);
}

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

Martin

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.IsValid" 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.Page, IValidator
{
protected System.Web.UI.WebControls.Button btnValidate;
private bool isValid = true;
private string errorMessage = "";

void IValidator.Validate()
{
string functionName = "IValidator.Validate()";
System.Web.HttpContext.Current.Trace.Warn(functionName,"Entering");
isValid = false;//OK
//IsValid = false;//Compiler error -- System.Web.UI.Page.IsValid is Read
Only
System.Web.HttpContext.Current.Trace.Warn(functionName,"Exiting");
}//Validate

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

private void Page_Load(object sender, System.EventArgs e)
{
string functionName = "Page_Load";
System.Web.HttpContext.Current.Trace.Warn(functionName,"Entering");
System.Web.HttpContext.Current.Trace.Warn(functionName,"Exiting");
}
public string ErrorMessage
{
get { return errorMessage; }
set { errorMessage = value; }
}//ErrorMessage

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
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 InitializeComponent()
{
this.btnValidate.Click += new
System.EventHandler(this.btnValidate_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnValidate_Click(object sender, System.EventArgs e)
{
string functionName = "btnValidate_Click";
System.Web.HttpContext.Current.Trace.Warn(functionName,"Entering");
if (Page.IsValid == true)
{
Response.Write("Page is Valid!");
}
else
{
Response.Write("Page is <b>NOT</b> Valid!");
}
System.Web.HttpContext.Current.Trace.Warn(functionName,"Exiting");
}

protected override void OnUnload(EventArgs e)
{
string functionName = "OnUnload";
System.Web.HttpContext.Current.Trace.Warn(functionName,"Entering");
System.Web.HttpContext.Current.Trace.Warn(functionName,"Exiting");
if (Page != null)
{
Page.Validators.Remove(this);
}
base.OnUnload(e);
}

}
 
P

Peter Blum

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.Control). 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 CustomValidators. 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 CustomValidators 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 ValidationSummary that has hyperlinks on each
error message to set focus to the field associated with that error message.

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

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

Top