Inserting Controls Dynamically - with Validators Also Added Dynamically

Discussion in 'ASP .Net' started by Jeffrey Todd, Jun 1, 2005.

  1. Jeffrey Todd

    Jeffrey Todd Guest

    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 dynamically, for some of the inserted
    controls.

    The controls (e.g., textBoxes) get created correctly and viewstate is
    maintained across postbacks, etc - BUT there is an issue with the validation
    controls.

    The validation controls, themselves are working - EXCEPT that
    1. They do their job only during a Postback (i.e., the page must actually
    post back in order for validation to occur); that is, client-side validation
    is not happening (i.e., it's not preventing a postback when it should).

    2. The validation summary control is apparently not working at all. It
    should pop up a message box, but it does nothing.

    My question is how to resolve/fix these two issues: I want the validation to
    occur client-side (in addition to the default server-side validation), and I
    want the validation summary control to work.

    FWIW here is my code:
    Hosting ASCX:
    <%@ Control CodeBehind="ucForm1.ascx.cs" Language="c#"
    AutoEventWireup="false" Inherits="MyAssembly.ucForm1" %>
    <asp:placeHolder id="FormPlaceHolder" runat="server"></asp:placeHolder>
    <table align="center"><tr><td><asp:Button id="btnSave"
    EnableViewState="false" CommandName="btnSave" Visible="true" Width="100px"
    runat="server" Text="Save"></asp:Button></td></tr></table>

    Relevant Code Behind:
    The method named InsertDynamicControls() is called from the OnInit() event
    ucForm1.

    private void InsertDynamicControls () {
    Literal spacer = new Literal();
    spacer.Text = "&nbsp;";

    // This is where I have all the logic that inserts the FORM controls into
    the Placeholder
    string s = "<table border=2><tr><td>Yo</td><tr><td>";
    s += "<asp:TextBox id=\"txtBox1\" EnableViewState=\"false\"
    Width=\"300px\" runat=\"server\"></asp:TextBox>";
    s += "</td></tr></tr><tr><td>Some Value</td></tr><tr><td>Other
    Value</td></tr></table>";

    System.Web.UI.Control c = ParseControl(s);

    FormPlaceHolder.Controls.Add(c);

    RequiredFieldValidator requiredFieldValidator = new
    RequiredFieldValidator();
    requiredFieldValidator.ControlToValidate = "txtBox1";
    requiredFieldValidator.EnableClientScript = true;
    requiredFieldValidator.Enabled = true;
    requiredFieldValidator.ErrorMessage = "Yo - You gotta enter something
    man!";
    requiredFieldValidator.ID = "RequiredFieldValidator_Title";
    requiredFieldValidator.Text = "*";
    requiredFieldValidator.EnableClientScript = true;

    FormPlaceHolder.Controls.Add(spacer);

    FormPlaceHolder.Controls.Add(requiredFieldValidator);

    ValidationSummary validationSummary = new ValidationSummary();
    validationSummary.DisplayMode =
    System.Web.UI.WebControls.ValidationSummaryDisplayMode.BulletList;
    validationSummary.ShowMessageBox = true;
    validationSummary.HeaderText = "These things must be fixed before you can
    proceed:";
    validationSummary.ShowSummary = false;
    validationSummary.ID = "validSummary1";
    validationSummary.EnableClientScript = true;

    FormPlaceHolder.Controls.Add(spacer);
    FormPlaceHolder.Controls.Add(validationSummary);
    }

    Thanks!
    Jeffrey Todd, Jun 1, 2005
    #1
    1. Advertising

  2. Jeffrey Todd

    Peter Blum Guest

    The overall code looks fine, except you are adding one control (spacer) in
    multiple places (which I thought was illegal). So I wonder if the
    WebUIValidation.js script file simply isn't loading.

    Take a look at this thread for several ideas:
    http://forums.asp.net/739537/ShowPost.aspx

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    "Jeffrey Todd" <> wrote in message
    news:%...
    >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 dynamically, for some of the
    >inserted controls.
    >
    > The controls (e.g., textBoxes) get created correctly and viewstate is
    > maintained across postbacks, etc - BUT there is an issue with the
    > validation controls.
    >
    > The validation controls, themselves are working - EXCEPT that
    > 1. They do their job only during a Postback (i.e., the page must actually
    > post back in order for validation to occur); that is, client-side
    > validation is not happening (i.e., it's not preventing a postback when it
    > should).
    >
    > 2. The validation summary control is apparently not working at all. It
    > should pop up a message box, but it does nothing.
    >
    > My question is how to resolve/fix these two issues: I want the validation
    > to occur client-side (in addition to the default server-side validation),
    > and I want the validation summary control to work.
    >
    > FWIW here is my code:
    > Hosting ASCX:
    > <%@ Control CodeBehind="ucForm1.ascx.cs" Language="c#"
    > AutoEventWireup="false" Inherits="MyAssembly.ucForm1" %>
    > <asp:placeHolder id="FormPlaceHolder" runat="server"></asp:placeHolder>
    > <table align="center"><tr><td><asp:Button id="btnSave"
    > EnableViewState="false" CommandName="btnSave" Visible="true" Width="100px"
    > runat="server" Text="Save"></asp:Button></td></tr></table>
    >
    > Relevant Code Behind:
    > The method named InsertDynamicControls() is called from the OnInit() event
    > ucForm1.
    >
    > private void InsertDynamicControls () {
    > Literal spacer = new Literal();
    > spacer.Text = "&nbsp;";
    >
    > // This is where I have all the logic that inserts the FORM controls into
    > the Placeholder
    > string s = "<table border=2><tr><td>Yo</td><tr><td>";
    > s += "<asp:TextBox id=\"txtBox1\" EnableViewState=\"false\"
    > Width=\"300px\" runat=\"server\"></asp:TextBox>";
    > s += "</td></tr></tr><tr><td>Some Value</td></tr><tr><td>Other
    > Value</td></tr></table>";
    >
    > System.Web.UI.Control c = ParseControl(s);
    >
    > FormPlaceHolder.Controls.Add(c);
    >
    > RequiredFieldValidator requiredFieldValidator = new
    > RequiredFieldValidator();
    > requiredFieldValidator.ControlToValidate = "txtBox1";
    > requiredFieldValidator.EnableClientScript = true;
    > requiredFieldValidator.Enabled = true;
    > requiredFieldValidator.ErrorMessage = "Yo - You gotta enter something
    > man!";
    > requiredFieldValidator.ID = "RequiredFieldValidator_Title";
    > requiredFieldValidator.Text = "*";
    > requiredFieldValidator.EnableClientScript = true;
    >
    > FormPlaceHolder.Controls.Add(spacer);
    >
    > FormPlaceHolder.Controls.Add(requiredFieldValidator);
    >
    > ValidationSummary validationSummary = new ValidationSummary();
    > validationSummary.DisplayMode =
    > System.Web.UI.WebControls.ValidationSummaryDisplayMode.BulletList;
    > validationSummary.ShowMessageBox = true;
    > validationSummary.HeaderText = "These things must be fixed before you can
    > proceed:";
    > validationSummary.ShowSummary = false;
    > validationSummary.ID = "validSummary1";
    > validationSummary.EnableClientScript = true;
    >
    > FormPlaceHolder.Controls.Add(spacer);
    > FormPlaceHolder.Controls.Add(validationSummary);
    > }
    >
    > Thanks!
    >
    Peter Blum, Jun 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian H
    Replies:
    2
    Views:
    570
    Christian H
    Jul 23, 2003
  2. Ratman
    Replies:
    0
    Views:
    652
    Ratman
    Sep 14, 2004
  3. tshad
    Replies:
    6
    Views:
    430
    tshad
    Jan 27, 2005
  4. David Hubbard
    Replies:
    2
    Views:
    624
    David Hubbard
    Jan 17, 2006
  5. ENathan

    existing controls overlaying dynamically added controls

    ENathan, Jan 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    196
    Steve C. Orr [MVP, MCSD]
    Jan 10, 2005
Loading...

Share This Page