Problem with ControlToValidate in dynamic create validation controls
<p>Hi<br>
I have site with two text boxes and dynamically create validation control. This is code from .aspx file:  </p>

<pre><code>&lt;form runat="server"&gt;  
&lt;asp:TextBox AutoPostBack="true" ID="TextBox1" Text="" runat="server" Width="200px"
OnTextChanged="TextBox1_TextChanged"&gt;&lt;/asp:TextBox&gt;  
  &lt;asp:TextBox ID="TextBox2" runat="server" Visible="True" Width="200px"AutoPostBack="true"&gt;&lt;/asp:TextBox&gt;  
&lt;asp:Panel ID="Panel1" runat="server"&gt;
&lt;/asp:Panel&gt;  
&lt;asp:TextBox ID="ValidationTB" runat="server" Visible="true"&gt;&lt;/asp:TextBox&gt;  
&lt;/form&gt;  
</code></pre>

<p>This is my code-behind:  </p>

<pre><code>public partial class _Default : System.Web.UI.Page  
{  
protected void Page_Load(object sender, EventArgs e)
{
    if (TextBox2.Visible)
    {
        if (!String.IsNullOrEmpty(TextBox1.Text) &amp;&amp; String.IsNullOrEmpty(TextBox2.Text))
        {
            RequiredFieldValidator RequiredFieldValidator1 = new RequiredFieldValidator();
            RequiredFieldValidator1.Enabled = true;
            RequiredFieldValidator1.ErrorMessage = "Second field required";
            RequiredFieldValidator1.Display = ValidatorDisplay.Dynamic;                
            RequiredFieldValidator1.ControlToValidate = "TextBox2";
            Panel1.Controls.Add(RequiredFieldValidator1);
            RequiredFieldValidator1.Validate();
        }
        if (!String.IsNullOrEmpty(TextBox2.Text) &amp;&amp; String.IsNullOrEmpty(TextBox1.Text))
        {
            RequiredFieldValidator RequiredFieldValidator1 = new RequiredFieldValidator();
            RequiredFieldValidator1.Enabled = true;
            RequiredFieldValidator1.ErrorMessage = "First field required";
            RequiredFieldValidator1.Display = ValidatorDisplay.Dynamic;
            RequiredFieldValidator1.ControlToValidate = "TextBox1";
            Panel1.Controls.Add(RequiredFieldValidator1);
            RequiredFieldValidator1.Validate();
        }
        if (!String.IsNullOrEmpty(TextBox2.Text) &amp;&amp; !String.IsNullOrEmpty(TextBox1.Text))
        {
            if (Convert.ToDateTime(TextBox2.Text) &lt; Convert.ToDateTime(TextBox1.Text))
            {
                ValidationTB.Text = null;
                RequiredFieldValidator RequiredFieldValidator1 = new RequiredFieldValidator();
                RequiredFieldValidator1.Enabled = true;
                RequiredFieldValidator1.ErrorMessage = "Bad range of dates";
                RequiredFieldValidator1.Display = ValidatorDisplay.Dynamic;                    
                RequiredFieldValidator1.ControlToValidate = "ValidationTB";
                Panel1.Controls.Add(RequiredFieldValidator1);
                RequiredFieldValidator1.Validate();
            }
        }
    }
}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    RegularExpressionValidator RegularExpressionValidator1 = new RegularExpressionValidator();
    RegularExpressionValidator1.ValidationExpression = @"^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
    RegularExpressionValidator1.Enabled = true;
    RegularExpressionValidator1.ErrorMessage = "Bad format of date";
    RegularExpressionValidator1.Display = ValidatorDisplay.Dynamic;

    if (!String.IsNullOrEmpty(TextBox1.Text))
    {
        RegularExpressionValidator1.ControlToValidate = "TextBox1";
        Panel1.Controls.Add(RegularExpressionValidator1);
        RegularExpressionValidator1.Validate();
    }
    if (!String.IsNullOrEmpty(TextBox2.Text))
    {
        RegularExpressionValidator1.ControlToValidate = "TextBox2";
        Panel1.Controls.Add(RegularExpressionValidator1);
        RegularExpressionValidator1.Validate();
    }
}
</code></pre>

<p>}</p>

<p>TextBox ValidationTB is just to make validate on empty control.<br>
This validation doesn't work, when I try:<br>
1. To first textbox enter for example: 2009-09-09<br>
2. To second textbox enter for example: 2009-10-09<br>
Now, everything is OK.<br>
3. I change my first textbox on for example 2009-12-09<br>
I get error Bad range of dates - it's OK.<br>
4. I correct first textbox on 2009-09-09, message disappear-OK.<br>
5. Again enter to first textbox 2009-12-09 - I don't have error, but it should be.<br>
What strange - in debug mode I can see, that in code:  </p>

<pre><code>            if (Convert.ToDateTime(TextBox2.Text) &lt; Convert.ToDateTime(TextBox1.Text))
            {
                ValidationTB.Text = null;
                RequiredFieldValidator RequiredFieldValidator1 = new RequiredFieldValidator();
                RequiredFieldValidator1.Enabled = true;
                RequiredFieldValidator1.ErrorMessage = "Bad range of dates";
                RequiredFieldValidator1.Display = ValidatorDisplay.Dynamic;                    
                RequiredFieldValidator1.ControlToValidate = "ValidationTB";
                Panel1.Controls.Add(RequiredFieldValidator1);
                //In debug window: RequiredFieldValidator1.ControlToValidate = "TextBox2"
                RequiredFieldValidator1.Validate();
            }  
</code></pre>

<p>instead of ValidationTB control, RequiredFieldValidator1.ControlToValidate is set to TextBox2 (it isn't empty, so I haven't error message).<br>
Why TextBox2 is set to RequiredFieldValidator1.ControlToValidate instead of ValidationTB textbox and how I could solve this?<br>
Thanks<br>
Regards</p>
