Add RequiredFieldValidator to dynamically created control in code behind
<p>I have a page with some textboxes.  Some of the ASP:Textboxes are made in the .aspx file, and other custom ones are created in codebehind (based on db).  I want to validate to make sure all fields are filled.</p>

<p>When I press my ASP:Button to continue, the textboxes created in my html/aspx file are correctly validated, but for some reason, the dynamically created RequiredFieldValidators are ignored.</p>

<p>Code:</p>

<pre><code>                    &lt;tr runat="server" id="weight_row"&gt;
                        &lt;td&gt;
                            Weight: &lt;asp:Label runat="server" ID="weightReqLabel" CssClass="required" Visible="false"&gt;*&lt;/asp:Label&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            &lt;asp:TextBox runat="server" ID="weight" CssClass="form-control"&gt;&lt;/asp:TextBox&gt;
                            &lt;asp:RequiredFieldValidator runat="server" ID="validator_weight" ControlToValidate="weight" SetFocusOnError="true" ErrorMessage="Required." CssClass="validation-error"&gt;&lt;/asp:RequiredFieldValidator&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
</code></pre>

<p>The above works correctly.</p>

<p>However, this does not work:</p>

<pre><code>TextBox txtbx = new TextBox();
txtbx.ID = "TextBox_" + i;
txtbx.CssClass = "form-control";
txtbx.Text = fieldValue;

RequiredFieldValidator reqFieldVal = new RequiredFieldValidator();
reqFieldVal.ID = "validator_" + i;
reqFieldVal.ControlToValidate = txtbx.ID;
reqFieldVal.SetFocusOnError = true;
reqFieldVal.ErrorMessage = "Required";
reqFieldVal.EnableClientScript = false;
reqFieldVal.CssClass = "required";
reqFieldVal.Enabled = true;

placeHolder.Controls.Add(txtbx);
placeHolder.Controls.Add(reqFieldVal);
</code></pre>

<p>Before I added <code>EnableClientScript=false</code>, none of the validation was working.  After I added that, the normally coded validators worked, but the code behind ones do not.  </p>

<p>I've also tried to add a ValidationGroup and try Page.Validate in the OnClick of the button but it doesn't seem to help.</p>

<pre><code>protected void SaveAndContinue_Click(object sender, EventArgs e)
{
    Page.Validate();
        if (Page.IsValid)
        {
            ((Redirect))
        }

}
</code></pre>

<p>What am I doing wrong?</p>