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
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