Code Comments
Programming Forum and web based access to our favorite programming groups.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(objec
t 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
Post Follow-up to this messageHi Madhur, Try setting the ValidatorGroup Property on Button1 instead of textbox. From the ASP.Net doc at http://msdn2.microsoft.com/en-us/li...validator.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 someon e > 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 ea ch > 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 validati on > 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(objec t 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.