title: Enabling RequiredFieldValidator From Code Behind
body: <p>I'm displaying an error message for the requiredfieldvalidator from code behind but the display is set to none or visible is hidden. I tried to do .visible = true but it's still hidden. How can I make it to show the error message? The textbox is inside a GridView.</p>
  
  <pre><code>&lt;asp:TemplateField HeaderText="SSN"&gt;
      &lt;ItemTemplate&gt;
      &lt;asp:TextBox ID="txtSSN" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSSN" /&gt;
       &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  
  TextBox txtSSN = (TextBox)r.FindControl("txtSSN");
  if (txtSSN.Text == "")
          {
        RequiredFieldValidator RequiredFieldValidator1 = (RequiredFieldValidator)r.FindControl("RequiredFieldValidator1");
        RequiredFieldValidator1.Visible = true;
        RequiredFieldValidator1.Enabled = true;
        RequiredFieldValidator1.ErrorMessage = "This field is required.";
          }
  </code></pre>