How to use ASP.NET validators

M

Madhur

Hello All

I am learning how to use ASP.NET Validators and would appreciate if someone
could provide me with guidance.

I have written very simple ASPX page below with a Dropdown list, a button.
If a value of 3 is selected inside dropdown list , I add two text boxes each
attached with validators.


Although validators work and do show th error messages in Red if the range
is outside 10, the call to the function Page.IsValid always returns TRUE
inside my button event handler. It should return false, since the validation
has false, isn't it?


using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
TextBox txtbox;
protected void Page_PreInit(object sender, EventArgs e)
{
DropDownList1.AutoPostBack = true;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.Items.Add("1");
DropDownList1.Items.Add("2");
DropDownList1.Items.Add("3");


}
else
{
AddControls();

}
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder displayValues = new
System.Text.StringBuilder();
if (Page.IsValid)
{
int i = 10; //this event always fires
}
Page.Validate();

}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{

// AddControls();

}

private void AddControls()
{
if (DropDownList1.SelectedIndex == 2)
{
RangeValidator rv;

for (int i = 0; i < 2; ++i)
{
txtbox = new TextBox();
txtbox.ID = "madhur" + i.ToString();
txtbox.ValidationGroup = "madhur";

rv = new RangeValidator();
rv.ID = "validator" + i.ToString();
rv.EnableClientScript = false;
rv.Text = "*";
rv.MinimumValue = "10";
rv.MaximumValue = "10";
rv.EnableClientScript = true;
rv.ErrorMessage = "this is an error";
rv.Display = ValidatorDisplay.Dynamic;
rv.ControlToValidate = "madhur" + i.ToString();
rv.Type = ValidationDataType.Integer;
rv.Enabled = true;
rv.ValidationGroup = "madhur";
rv.SetFocusOnError = true;

this.form1.Controls.Add(txtbox);
this.form1.Controls.Add(rv);



}
}
}
}

Thanks,
Madhur
 
W

wisccal

Hi Madhur,

Try setting the ValidatorGroup Property on Button1 instead of textbox.
From the ASP.Net doc at http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.aspx:

"These controls each have a ValidationGroup property that, when set,
validates only the validation controls within the specified group when
the control triggers a post back to the server."

The key part being "...when the control triggers a post back to the
server." So, you need to set the ValidatorGroup Property on the
control that posts back to the server. You already associated the
control that has to be checked with its Validator by setting
rv.ControlToValidate.

Of course, you can also call the Page.Validate() method to trigger
validation in the codebehind. But then, you need to do it before you
check Page.IsValid. In your Button1_Click event, you do it the other
way around.

===========
Regards,
Steve
www.stkomp.com
 
H

Hans Kesting

Madhur used his keyboard to write :
Hello All

I am learning how to use ASP.NET Validators and would appreciate if someone
could provide me with guidance.

I have written very simple ASPX page below with a Dropdown list, a button.
If a value of 3 is selected inside dropdown list , I add two text boxes each
attached with validators.


Although validators work and do show th error messages in Red if the range is
outside 10, the call to the function Page.IsValid always returns TRUE inside
my button event handler. It should return false, since the validation has
false, isn't it?


using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
TextBox txtbox;
protected void Page_PreInit(object sender, EventArgs e)
{
DropDownList1.AutoPostBack = true;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.Items.Add("1");
DropDownList1.Items.Add("2");
DropDownList1.Items.Add("3");


}
else
{
AddControls();

}
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder displayValues = new
System.Text.StringBuilder();
if (Page.IsValid)
{
int i = 10; //this event always fires
}
Page.Validate();

}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{

// AddControls();

}

private void AddControls()
{
if (DropDownList1.SelectedIndex == 2)
{
RangeValidator rv;

for (int i = 0; i < 2; ++i)
{
txtbox = new TextBox();
txtbox.ID = "madhur" + i.ToString();
txtbox.ValidationGroup = "madhur";

rv = new RangeValidator();
rv.ID = "validator" + i.ToString();
rv.EnableClientScript = false;
rv.Text = "*";
rv.MinimumValue = "10";
rv.MaximumValue = "10";
rv.EnableClientScript = true;
rv.ErrorMessage = "this is an error";
rv.Display = ValidatorDisplay.Dynamic;
rv.ControlToValidate = "madhur" + i.ToString();
rv.Type = ValidationDataType.Integer;
rv.Enabled = true;
rv.ValidationGroup = "madhur";
rv.SetFocusOnError = true;

this.form1.Controls.Add(txtbox);
this.form1.Controls.Add(rv);



}
}
}
}

Thanks,
Madhur

You seem to add a validator only on "SelectedIndexChanged" on the
pulldown (and then only if a particular value is selected).
This will only add that validator for this particular request.
When you then process the Click event on the button, the validator is
not present anymore. This means the page *is* valid, as there are no
validators that complain.
The same goes for the textbox.

When you add controls dynamically, you have to add them on every
request. And you need to do this re-adding in the Load event or sooner.
You could set some value in ViewState to indicate that those particular
controls are required.

Hans Kesting
 
M

Madhur

Hans Kesting said:
Madhur used his keyboard to write :

You seem to add a validator only on "SelectedIndexChanged" on the pulldown
(and then only if a particular value is selected).
This will only add that validator for this particular request.
When you then process the Click event on the button, the validator is not
present anymore. This means the page *is* valid, as there are no
validators that complain.
The same goes for the textbox.

When you add controls dynamically, you have to add them on every request.
And you need to do this re-adding in the Load event or sooner.
You could set some value in ViewState to indicate that those particular
controls are required.

Hans Kesting

Hi Hans

Thanks for the excellent explanation. Now the point is:

* When I am pressing the click Button, the form has been posted with the
data with the values of textboxes and Validators.
So the Page.IsValid should indeed return false, although the controls are
not really present after the postback, but that should not matter.

* The other post seems to correct it by assinging the validation group to
the button, it works.

I buy your idea of indicating in ViewState, to indicate which controls(which
were dynamically created) should be recreated after request.
But What about there values ? , Do I also have to manually store it in
viewstate and restore them in Load event.

Is there any official article on it, may be on MSDN on how to handle these
kind of scenarios?

Thanks,
Madhur
 
W

wisccal

Hi Madhur,

In the code you posted, you call AddControls() on every postback, so,
if that is correct, I don't see any problem with Validators not being
present.

It is true that, if you dynamically create controls, you need to
recreate them on every postback, as ASP won't do that for you. The
data, though, will be saved to and loaded from ViewState
automatically, so you don't need to take care of that.

Here is a good read on the subject:
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

Reading your post, I'm not 100% sure if you were able to resolve your
problem. If not, please post back.

===============
Regards,
Steve
www.stkomp.com
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top