473,406 Members | 2,894 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,406 software developers and data experts.

How to use ASP.NET validators

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


Jun 27 '08 #1
4 1252
Hi Madhur,

Try setting the ValidatorGroup Property on Button1 instead of textbox.
From the ASP.Net doc at http://msdn2.microsoft.com/en-us/lib...alidator.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

Madhur wrote:
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
Jun 27 '08 #2
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
Jun 27 '08 #3

"Hans Kesting" <in************@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
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

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

Jun 27 '08 #4
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

On Apr 19, 4:45 am, "Madhur" <s...@df.comwrote:
"Hans Kesting" <invalid.han...@spamgourmet.comwrote in message

news:mn***********************@spamgourmet.com...
Madhurused 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

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
Jun 27 '08 #5

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

Similar topics

1
by: MonkeyBoy | last post by:
I am doing some some HTML manipulation in client-side script (IE5.x and IE6.x browsers only). Something like.. var tmpHTML = oTable.outerHTML // do something to the HTML her oTable.outerHTML =...
1
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These...
0
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These...
7
by: angus | last post by:
Dear all, I have 5 textboxes, and 2 buttons in a webform. 1-3 textboxes would be validated by 3 Required Field Validators if button 1 is click; 4-5 textboxes would be validated by 2 Required...
6
by: Mark | last post by:
We have Validators embedded in an asp table server control. The table server control is necessary and cannot be replaced. We want to apply CSS formatting to the validators, but the validators...
3
by: John Blair | last post by:
Hi, I have validators outside of a datagrid (for adding a new grid row) - however when i click "edit" column and then the "update" column of a grid row that has been edited - my other...
1
by: epigram | last post by:
I'm trying to use the ASP.NET validators to check some client-side business rules. I've got two ASP TextBox controls (call them tbxYear1 and tbxYear2) used to enter a range of years. I've got a...
1
by: Gabriel Lozano-Morán | last post by:
When using the tabstrip control combined with a multipage (several pageview) there is a problem when using validators. The problem is that validation also occurs on the validators that are not on...
2
by: Mike Surcouf | last post by:
Hi I have some regex validators on my page set to dynamic and like the way they appear after you tab out of a field and also when you try to postback the form. All OK so far When I register...
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: 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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.