dynamically add controls and validators - always false

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default dynamically add controls and validators - always false

    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">
    <asp:placeholder 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);

    }

    }
    }




    Christian H Guest

  2. Similar Questions and Discussions

    1. Dynamically Adding Controls
      I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls...
    2. Nested controls in User Control via ParseChildren(false) not worki
      Hello, Using ASP.NET 2.0/Visual Studio 2005 Beta 2 and programming in C#, I'm trying to create a user control that allows nested content between...
    3. Access dynamically created controls
      Hi there, I read a lot about this issue but still got no clear answer that solves my problem. I've a Web User Control with a placeholder called...
    4. get formvalues - dynamically created controls
      C.H., You could loop through them using their index. Instead of using FindControl and the control's name you could just use the index of the...
    5. How do I dynamically create user controls?
      Thanks for any help...! My error is: Object reference not set to an instance of an object. Here's the setup: I have made a user control...
  3. #2

    Default Re: dynamically add controls and validators - always false

    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
    [url]http://www.mansoft.fi[/url]

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

    "Christian H" <no@name.no> wrote in message
    news:uDPTYxOUDHA.2420@TK2MSFTNGP10.phx.gbl...
    > 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">
    > <asp:placeholder 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);
    >
    > }
    >
    > }
    > }
    >
    >
    >
    >

    Teemu Keiski Guest

  4. #3

    Default Re: dynamically add controls and validators - always false

    Answered this on aspnet group and ASP.NET Forums.

    I got 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
    [url]http://www.mansoft.fi[/url]

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

    "Christian H" <no@name.no> wrote in message
    news:OhA40wOUDHA.2200@TK2MSFTNGP11.phx.gbl...
    > 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.
    >
    > I'm not sure what I need to do in order to make this work. Do I need to
    > create my own custom control?
    >
    > 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">
    > <asp:placeholder 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);
    >
    > }
    >
    > }
    > }
    >
    >
    >
    >
    >

    Teemu Keiski Guest

  5. #4

    Default thank you! - where can I learn more?

    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.



    Christian H Guest

  6. #5

    Default Re: thank you! - where can I learn more?

    The CreateChildControls overriding was just because Page inherits from
    System.Web.UI.Control that again uses CreateChildControls to ensure child
    controls are created in proper place and at proper time. Basically if you'd
    add those controls in Page_Init that would have done it as well.

    If you want book recommendation get "Developing ASP.NET Server Controls and
    Components" by MSPress.

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

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


    "Christian H" <no@name.no> wrote in message
    news:O75eQyPUDHA.2420@TK2MSFTNGP10.phx.gbl...
    > 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.
    >
    >
    >

    Teemu Keiski Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139