Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamically created validators - server side validation not working.

Reply
Thread Tools

Dynamically created validators - server side validation not working.

 
 
Tony
Guest
Posts: n/a
 
      12-12-2003
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"/>
<asplaceHolder 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);
}

}
}


 
Reply With Quote
 
 
 
 
Tony
Guest
Posts: n/a
 
      12-12-2003
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:VThCb.36513$(E-Mail Removed)...
> 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"/>
> <asplaceHolder 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);
> }
>
> }
> }
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
Validators - error message not showing when JS disabled & only server side validation occurs TS ASP .Net Web Controls 2 06-20-2006 10:00 AM
Server-side events do not fire the first time after client-side validators have fired Stuart Whiteford ASP .Net 2 04-25-2006 10:10 AM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
ASP.NET Web Forms Validation Controls are Server-Side or Client-Side Validation? Matt ASP .Net 14 01-30-2004 09:15 AM



Advertisments