Dynamically created validators - server side validation not working.

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

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);
}

}
}
 
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
 
Back
Top