Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > dynamically add controls and validators - always false

Reply
Thread Tools

dynamically add controls and validators - always false

 
 
Christian H
Guest
Posts: n/a
 
      07-23-2003
Based on the content in my database, I need to populate different form
controls and validators, such as textbox, dropdownlist,
requiredfieldvalidators , etc.

Then I need to check if the form has been filled out correctly. If so, I
need to update a database, and do other things.

In order to check if the form is ok, I've tried looping through the
validator controls, and I've also tried using page.isvalid.

Nomatter what, the form seems to be incorrrectly filled out. The validator
controls comes out as "false", and Page.isvalid also comes out as false.


Regards C.H




Here is a modified example of my problem, so that you can see what I'm
dealing with.

<%@ Page language="c#" Codebehind="testfil1.aspx.cs" AutoEventWireup="false"
Inherits="smbuClass.testfil1" Trace=true enableViewState=false %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>testfil1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form runat="server" ID="Form1" method="post" action="testfil1.aspx">
<asplaceholder id="plh" runat="server" />
<asp:button Text="Click" runat="server" CausesValidation="true"
ID="Button" />
</form>
</body>
</HTML>

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace smbuClass
{
public class testfil1 : System.Web.UI.Page
{

protected RequiredFieldValidator validator2 = new
RequiredFieldValidator();
protected TextBox txtfield=new TextBox();
protected System.Web.UI.WebControls.Button Button;
protected PlaceHolder plh;

private void Page_Load(Object sender,EventArgs e)
{

txtfield.ID="txtfield";
plh.Controls.Add(txtfield);

validator2.ControlToValidate=txtfield.ID+"";
validator2.ID="validator2";
validator2.Text = "Error";
validator2.ErrorMessage="Error";
validator2.EnableClientScript=false;
plh.Controls.Add(validator2);


if(Page.IsPostBack)
{
Page.Validate();
if(Page.IsValid)
Response.Write("all ok");
else
Response.Write("not ok");

foreach(BaseValidator b in Page.Validators)
{
Trace.Warn(b.IsValid + " " +b.ID + " ");
}

}





}


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

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}

}
}




 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-23-2003
I go track of the error now.

Change the code like this:


protected override void CreateChildControls()

{

RequiredFieldValidator validator2 = new RequiredFieldValidator();



TextBox txtfield=new TextBox();

txtfield.ID="txtfield";

plh.Controls.Add(txtfield);



plh.Controls.Add(validator2);

validator2.ControlToValidate=txtfield.ID;

validator2.ID="validator2";

validator2.Text = "Error";

validator2.ErrorMessage="Error";

validator2.EnableClientScript=false;

}



Just add this overridden CreateChildControls to the Page and remove the
relevant code from Page_Load. That should do it. (Page.Validate etc can
still be where they are now)

--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com

"Christian H" <> wrote in message
news:...
> Based on the content in my database, I need to populate different form
> controls and validators, such as textbox, dropdownlist,
> requiredfieldvalidators , etc.
>
> Then I need to check if the form has been filled out correctly. If so, I
> need to update a database, and do other things.
>
> In order to check if the form is ok, I've tried looping through the
> validator controls, and I've also tried using page.isvalid.
>
> Nomatter what, the form seems to be incorrrectly filled out. The validator
> controls comes out as "false", and Page.isvalid also comes out as false.
>
>
> Regards C.H
>
>
>
>
> Here is a modified example of my problem, so that you can see what I'm
> dealing with.
>
> <%@ Page language="c#" Codebehind="testfil1.aspx.cs"

AutoEventWireup="false"
> Inherits="smbuClass.testfil1" Trace=true enableViewState=false %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
> <HTML>
> <HEAD>
> <title>testfil1</title>
> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
> <meta name="CODE_LANGUAGE" Content="C#">
> <meta name="vs_defaultClientScript" content="JavaScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> </HEAD>
> <body MS_POSITIONING="GridLayout">
> <form runat="server" ID="Form1" method="post" action="testfil1.aspx">
> <asplaceholder id="plh" runat="server" />
> <asp:button Text="Click" runat="server" CausesValidation="true"
> ID="Button" />
> </form>
> </body>
> </HTML>
>
> using System;
> using System.Data;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Collections;
>
> namespace smbuClass
> {
> public class testfil1 : System.Web.UI.Page
> {
>
> protected RequiredFieldValidator validator2 = new
> RequiredFieldValidator();
> protected TextBox txtfield=new TextBox();
> protected System.Web.UI.WebControls.Button Button;
> protected PlaceHolder plh;
>
> private void Page_Load(Object sender,EventArgs e)
> {
>
> txtfield.ID="txtfield";
> plh.Controls.Add(txtfield);
>
> validator2.ControlToValidate=txtfield.ID+"";
> validator2.ID="validator2";
> validator2.Text = "Error";
> validator2.ErrorMessage="Error";
> validator2.EnableClientScript=false;
> plh.Controls.Add(validator2);
>
>
> if(Page.IsPostBack)
> {
> Page.Validate();
> if(Page.IsValid)
> Response.Write("all ok");
> else
> Response.Write("not ok");
>
> foreach(BaseValidator b in Page.Validators)
> {
> Trace.Warn(b.IsValid + " " +b.ID + " ");
> }
>
> }
>
>
>
>
>
> }
>
>
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
>
> private void InitializeComponent()
> {
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
>
> }
> }
>
>
>
>



 
Reply With Quote
 
 
 
 
Christian H
Guest
Posts: n/a
 
      07-23-2003
Thank you!

I need some advice on what to read in order to learn more about these
things.
Why did I need to override that method, and how will I know when I will need
to override a method next time...?

I've got a feeling I'm going to need to override methods a lot with this
project
Do you have an book suggestion, or online tutorials that will educate me on
this?

Regards Christian H.


 
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
False positive, false intrusion, false alarm Nick Computer Security 3 04-26-2006 07:40 PM
Inserting Controls Dynamically - with Validators Also Added Dynamically Jeffrey Todd ASP .Net 1 06-02-2005 04:33 PM
dynamically creating controls, validators, using autopost back =?Utf-8?B?TFU=?= ASP .Net 3 03-30-2005 01:21 PM
false instance and false class and set_trace_func trans. (T. Onoma) Ruby 0 09-25-2004 05:34 PM
Regular Expression validators NOT working, Required Field validators ARE working Ratman ASP .Net 0 09-14-2004 09:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57