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.Configur ation;
using System.Data;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.H tmlControls;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
public partial class _Default : System.Web.UI.P age
{
TextBox txtbox;
protected void Page_PreInit(ob ject sender, EventArgs e)
{
DropDownList1.A utoPostBack = true;
}
protected void Page_Load(objec t sender, EventArgs e)
{
if (!Page.IsPostBa ck)
{
DropDownList1.I tems.Add("1");
DropDownList1.I tems.Add("2");
DropDownList1.I tems.Add("3");
}
else
{
AddControls();
}
}
protected void Button1_Click(o bject sender, EventArgs e)
{
System.Text.Str ingBuilder displayValues = new
System.Text.Str ingBuilder();
if (Page.IsValid)
{
int i = 10; //this event always fires
}
Page.Validate() ;
}
protected void DropDownList1_S electedIndexCha nged(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.Validati onGroup = "madhur";
rv = new RangeValidator( );
rv.ID = "validator" + i.ToString();
rv.EnableClient Script = false;
rv.Text = "*";
rv.MinimumValue = "10";
rv.MaximumValue = "10";
rv.EnableClient Script = true;
rv.ErrorMessage = "this is an error";
rv.Display = ValidatorDispla y.Dynamic;
rv.ControlToVal idate = "madhur" + i.ToString();
rv.Type = ValidationDataT ype.Integer;
rv.Enabled = true;
rv.ValidationGr oup = "madhur";
rv.SetFocusOnEr ror = true;
this.form1.Cont rols.Add(txtbox );
this.form1.Cont rols.Add(rv);
}
}
}
}
Thanks,
Madhur 4 1261
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.ControlToVal idate.
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.Configur ation;
using System.Data;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.H tmlControls;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
public partial class _Default : System.Web.UI.P age
{
TextBox txtbox;
protected void Page_PreInit(ob ject sender, EventArgs e)
{
DropDownList1.A utoPostBack = true;
}
protected void Page_Load(objec t sender, EventArgs e)
{
if (!Page.IsPostBa ck)
{
DropDownList1.I tems.Add("1");
DropDownList1.I tems.Add("2");
DropDownList1.I tems.Add("3");
}
else
{
AddControls();
}
}
protected void Button1_Click(o bject sender, EventArgs e)
{
System.Text.Str ingBuilder displayValues = new
System.Text.Str ingBuilder();
if (Page.IsValid)
{
int i = 10; //this event always fires
}
Page.Validate() ;
}
protected void DropDownList1_S electedIndexCha nged(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.Validati onGroup = "madhur";
rv = new RangeValidator( );
rv.ID = "validator" + i.ToString();
rv.EnableClient Script = false;
rv.Text = "*";
rv.MinimumValue = "10";
rv.MaximumValue = "10";
rv.EnableClient Script = true;
rv.ErrorMessage = "this is an error";
rv.Display = ValidatorDispla y.Dynamic;
rv.ControlToVal idate = "madhur" + i.ToString();
rv.Type = ValidationDataT ype.Integer;
rv.Enabled = true;
rv.ValidationGr oup = "madhur";
rv.SetFocusOnEr ror = true;
this.form1.Cont rols.Add(txtbox );
this.form1.Cont rols.Add(rv);
}
}
}
}
Thanks,
Madhur
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.Configur ation;
using System.Data;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.H tmlControls;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
public partial class _Default : System.Web.UI.P age
{
TextBox txtbox;
protected void Page_PreInit(ob ject sender, EventArgs e)
{
DropDownList1.A utoPostBack = true;
}
protected void Page_Load(objec t sender, EventArgs e)
{
if (!Page.IsPostBa ck)
{
DropDownList1.I tems.Add("1");
DropDownList1.I tems.Add("2");
DropDownList1.I tems.Add("3");
}
else
{
AddControls();
}
}
protected void Button1_Click(o bject sender, EventArgs e)
{
System.Text.Str ingBuilder displayValues = new
System.Text.Str ingBuilder();
if (Page.IsValid)
{
int i = 10; //this event always fires
}
Page.Validate() ;
}
protected void DropDownList1_S electedIndexCha nged(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.Validati onGroup = "madhur";
rv = new RangeValidator( );
rv.ID = "validator" + i.ToString();
rv.EnableClient Script = false;
rv.Text = "*";
rv.MinimumValue = "10";
rv.MaximumValue = "10";
rv.EnableClient Script = true;
rv.ErrorMessage = "this is an error";
rv.Display = ValidatorDispla y.Dynamic;
rv.ControlToVal idate = "madhur" + i.ToString();
rv.Type = ValidationDataT ype.Integer;
rv.Enabled = true;
rv.ValidationGr oup = "madhur";
rv.SetFocusOnEr ror = true;
this.form1.Cont rols.Add(txtbox );
this.form1.Cont rols.Add(rv);
}
}
}
}
Thanks,
Madhur
You seem to add a validator only on "SelectedIndexC hanged" 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
"Hans Kesting" <in************ @spamgourmet.co mwrote in message
news:mn******** *************** @spamgourmet.co m...
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.Configur ation; using System.Data; using System.Web; using System.Web.Secu rity; using System.Web.UI; using System.Web.UI.H tmlControls; using System.Web.UI.W ebControls; using System.Web.UI.W ebControls.WebP arts;
public partial class _Default : System.Web.UI.P age { TextBox txtbox; protected void Page_PreInit(ob ject sender, EventArgs e) { DropDownList1.A utoPostBack = true; }
protected void Page_Load(objec t sender, EventArgs e) { if (!Page.IsPostBa ck) { DropDownList1.I tems.Add("1"); DropDownList1.I tems.Add("2"); DropDownList1.I tems.Add("3");
} else { AddControls();
} } protected void Button1_Click(o bject sender, EventArgs e) { System.Text.Str ingBuilder displayValues = new System.Text.St ringBuilder(); if (Page.IsValid) { int i = 10; //this event always fires } Page.Validate() ;
} protected void DropDownList1_S electedIndexCha nged(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.Validati onGroup = "madhur";
rv = new RangeValidator( ); rv.ID = "validator" + i.ToString(); rv.EnableClient Script = false; rv.Text = "*"; rv.MinimumValue = "10"; rv.MaximumValue = "10"; rv.EnableClient Script = true; rv.ErrorMessage = "this is an error"; rv.Display = ValidatorDispla y.Dynamic; rv.ControlToVal idate = "madhur" + i.ToString(); rv.Type = ValidationDataT ype.Integer; rv.Enabled = true; rv.ValidationGr oup = "madhur"; rv.SetFocusOnEr ror = true;
this.form1.Cont rols.Add(txtbox ); this.form1.Cont rols.Add(rv); } } } }
Thanks, Madhur
You seem to add a validator only on "SelectedIndexC hanged" 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
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.comwro te:
"Hans Kesting" <invalid.han... @spamgourmet.co mwrote in message
news:mn******** *************** @spamgourmet.co m...
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.Configur ation;
using System.Data;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.H tmlControls;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
public partial class _Default : System.Web.UI.P age
{
TextBox txtbox;
protected void Page_PreInit(ob ject sender, EventArgs e)
{
DropDownList1.A utoPostBack = true;
}
protected void Page_Load(objec t sender, EventArgs e)
{
if (!Page.IsPostBa ck)
{
DropDownList1.I tems.Add("1");
DropDownList1.I tems.Add("2");
DropDownList1.I tems.Add("3");
}
else
{
AddControls();
}
}
protected void Button1_Click(o bject sender, EventArgs e)
{
System.Text.Str ingBuilder displayValues = new
System.Text.Str ingBuilder();
if (Page.IsValid)
{
int i = 10; //this event always fires
}
Page.Validate() ;
}
protected void DropDownList1_S electedIndexCha nged(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.Validati onGroup = "madhur";
rv = new RangeValidator( );
rv.ID = "validator" + i.ToString();
rv.EnableClient Script = false;
rv.Text = "*";
rv.MinimumValue = "10";
rv.MaximumValue = "10";
rv.EnableClient Script = true;
rv.ErrorMessage = "this is an error";
rv.Display = ValidatorDispla y.Dynamic;
rv.ControlToVal idate = "madhur" + i.ToString();
rv.Type = ValidationDataT ype.Integer;
rv.Enabled = true;
rv.ValidationGr oup = "madhur";
rv.SetFocusOnEr ror = true;
this.form1.Cont rols.Add(txtbox );
this.form1.Cont rols.Add(rv);
}
}
}
}
Thanks, Madhur
You seem to add a validator only on "SelectedIndexC hanged" 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 = tmpHTML
Before the changes, client-side .Net field validators (RegularExpressionValidator, RangeValidator, etc.) work fine... after the changes,...
|
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 controls and validators are then added to a table for display to the user.
This code did seem to work originally, but now does not amd I am stumped as to...
|
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 controls and validators are then added to a table for display to the user.
This code did seem to work originally, but now does not amd I am stumped as to...
|
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 Field Validators if button 2
is click.
I have found whether i click button 1 or button 2, textboxes 1-5 will be
|
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 have a default RED font
that appears to be difficult to remove. We could hard code in a blank font
color into the .aspx html for each validator, but...
| |
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 validators on the page fire which i don't want -
i've tried to make them invisible - this hides the controls but i get a
strange error when i click the...
|
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 couple things I need to check:
1) tbxYear1 and tbxYear2 are both optional, but if values are entered they
have to integers and non-negative
2) if...
|
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 the currently selected
pageview of the multipage. To prevent this from happening I disable all the
validators and only enable the validators on the...
|
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 for the client side event of a textbox using ...
textBox.Attributes.Add("onKeyUp","javascriptfunction");
|
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |