Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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







Report this thread to moderator Post Follow-up to this message
Old Post
Madhur
04-18-08 12:35 AM


Re: How to use ASP.NET validators
Hi 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

Report this thread to moderator Post Follow-up to this message
Old Post
wisccal@googlemail.com
04-18-08 10:06 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

ASP .NET archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:32 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.