title: how to set regular validation expression dynamically in codebehind C# ASP.NET
body: <p>I have <code>RegularExpressionValidator</code> in <code>ascx</code> page that validates <code>TextBox1</code>.</p>
  
  <pre><code>&lt;asp:RegularExpressionValidator ID ="RegularExpressionValidatorStateNumber" runat = "server" ControlToValidate="TextBox1" 
  ErrorMessage="Error message" Display ="Dynamic" ValidationExpression=""/&gt;
  </code></pre>
  
  <p>I need to change <code>ValidationExpression</code> dynamically depending on choice of<code>dropdownlist</code>.</p>
  
  <p>Here what I do in codeBehind:</p>
  
  <pre><code>protected void DropDlSelectedIndexChanged(object sender, EventArgs e)
  {        
      string region = DropDl.SelectedItem.Text;            
      RegularExpressionValidatorStateNumber.ValidationExpression = GetRegex(region); 
  }
  </code></pre>
  
  <p>Function <code>GetRegex(region)</code> that returns regex expression works fine.
  But validation not works. Validator doesn't show error message when wrong data inserted.</p>
  
  <p><em>Why validation not works? Or how to set validation expression dynamically?</em></p>