473,385 Members | 1,478 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,385 software developers and data experts.

Dynamically created validators - server side validation not working.

Hi folks,

I've got a bit of a problem. I have a situation where I build forms
completely dynamically based on a form definition supplied from a database.
Anyway, I noticed that required fields weren't validating on the server
side, so I've whittled the code down to the example below which illustrates
the problem.

Basically, when I programmatically create a control and a required field
validator, and don't use client side validation, the server side validation
is performed, but ignored - and my button's Click handler is still called. I
can obviously work around this by doing a if( ! Page.IsValid ) return in
the handler, but the validation system is supposed to ensure that if page
content isn't valid, the button's handler won't be called.....

In the example below, you'll see that the button handler DOES get called,
even though Page.IsValid is false. But here's something interesting - remove
the throw and the redirect from the handler, and you'll see that yes, the
handler still get's called, but when the processing has finished, the page
shows the errors as it should.....

anyway, the code is below, any help would be greatly appreciated, I'm about
to chuck my PC out the window with this one as I don't know whether I'm
doing something really daft or if I've discovered an "issue" in the
framework....

Cheers,
Tony

testit.aspx:
<%@ Page language="c#" Codebehind="valtest.aspx.cs" AutoEventWireup="false"
Inherits="trinity.admin.developer.sampler.valtest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<body>

<form id="Form1" method="post" runat="server">
<asp:ValidationSummary ID="_summary" ShowMessageBox="True" Runat="server"/>
<asp:PlaceHolder ID="_form" Runat="server"/>
<asp:Button ID="_save" Text="Save" Runat="server"/>
</form>
</body>
</HTML>
testit.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace trinity.admin.developer.sampler
{
/// <summary>
/// Summary description for valtest.
/// </summary>
public class valtest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.ValidationSummary _summary;
protected System.Web.UI.WebControls.PlaceHolder _form;
protected System.Web.UI.WebControls.Button _save;
protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
TextBox b = new TextBox();
b.ID = "theTextBox";
_form.Controls.Add(b);

RequiredFieldValidator v = new RequiredFieldValidator();
v.EnableClientScript = false;
v.ControlToValidate = "theTextBox";
v.ErrorMessage = "Problem";
v.ID = "thevalidator";

_form.Controls.Add(v);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this._save.Click += new System.EventHandler(this._save_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void _save_Click(object sender, System.EventArgs e)
{
if( ! Page.IsValid ) throw new Exception("I shouldn't get called here!");
Response.Redirect("/", true);
}

}
}
Nov 18 '05 #1
1 3466
Update::::

Would appear that this behaviour is right. I've just done the same thing,
but declaratively - set EnabledClientScript=false on the validator and the
same thing happens. Looks like you have to test Page.IsValid on your button
handlers!

Cheers,
Tony

"Tony" <reply_to@newsgroup_only.net> wrote in message
news:VT******************@news-binary.blueyonder.co.uk...
Hi folks,

I've got a bit of a problem. I have a situation where I build forms
completely dynamically based on a form definition supplied from a database. Anyway, I noticed that required fields weren't validating on the server
side, so I've whittled the code down to the example below which illustrates the problem.

Basically, when I programmatically create a control and a required field
validator, and don't use client side validation, the server side validation is performed, but ignored - and my button's Click handler is still called. I can obviously work around this by doing a if( ! Page.IsValid ) return in
the handler, but the validation system is supposed to ensure that if page
content isn't valid, the button's handler won't be called.....

In the example below, you'll see that the button handler DOES get called,
even though Page.IsValid is false. But here's something interesting - remove the throw and the redirect from the handler, and you'll see that yes, the
handler still get's called, but when the processing has finished, the page
shows the errors as it should.....

anyway, the code is below, any help would be greatly appreciated, I'm about to chuck my PC out the window with this one as I don't know whether I'm
doing something really daft or if I've discovered an "issue" in the
framework....

Cheers,
Tony

testit.aspx:
<%@ Page language="c#" Codebehind="valtest.aspx.cs" AutoEventWireup="false" Inherits="trinity.admin.developer.sampler.valtest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<body>

<form id="Form1" method="post" runat="server">
<asp:ValidationSummary ID="_summary" ShowMessageBox="True" Runat="server"/> <asp:PlaceHolder ID="_form" Runat="server"/>
<asp:Button ID="_save" Text="Save" Runat="server"/>
</form>
</body>
</HTML>
testit.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace trinity.admin.developer.sampler
{
/// <summary>
/// Summary description for valtest.
/// </summary>
public class valtest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.ValidationSummary _summary;
protected System.Web.UI.WebControls.PlaceHolder _form;
protected System.Web.UI.WebControls.Button _save;
protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
TextBox b = new TextBox();
b.ID = "theTextBox";
_form.Controls.Add(b);

RequiredFieldValidator v = new RequiredFieldValidator();
v.EnableClientScript = false;
v.ControlToValidate = "theTextBox";
v.ErrorMessage = "Problem";
v.ID = "thevalidator";

_form.Controls.Add(v);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this._save.Click += new System.EventHandler(this._save_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void _save_Click(object sender, System.EventArgs e)
{
if( ! Page.IsValid ) throw new Exception("I shouldn't get called here!"); Response.Redirect("/", true);
}

}
}

Nov 18 '05 #2

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

Similar topics

0
by: Trillium | last post by:
I am trying to upgrade a straight ASP project into .NET, but I am new at ..NET, and there seem to be so many different ways to do things. I am new to ..NET and need some guidance as to which...
4
by: Marina | last post by:
Hi, A very weird thing has happened. Today, all the validators on all the pages and user controls on one of the servers stopped working. They seem to do the validation server side, instead of...
2
by: Jason Shohet | last post by:
We coppied our site up to a 2003 remote server, and our validators stopped working. I read on another posting, that the validation mechanism works by examining some validation.js file in the web...
0
by: interuser | last post by:
I have an existing large application that uses validators. I have a problem with the client side validators not working on netscape (and any browser other than IE). As you may already know, the...
1
by: Jeffrey Todd | last post by:
I have successfully created functionality that mostly models what I'm trying to do - which is dynamically insert controls into a user control (ascx), and insert validation controls, also...
5
by: Jason | last post by:
I am able fire my field validators in FireFox on the server side using... Page.Validate() If Not Page.IsValid Then Exit Sub End If But the it won't fire Custom Validators. Anybody know why?
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...
8
by: ShaneFowlkes | last post by:
For some reason, my validator controls stopped working when a form posts. They seems to work on fields that lose focus but if I submit a form with all empty fields, not of the required validators...
1
by: Andrew Jocelyn | last post by:
Hi I have a Formview control in a UserControl. The server-side validation is not working, i.e. the events are not firing when a button control which causes validation is fired or even when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.