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.
<asp:TemplateField HeaderText="SSN">
<ItemTemplate>
<asp:TextBox ID="txtSSN" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSSN" />
</ItemTemplate>
</asp:TemplateField>
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.";
}
Set the IsValid
property to false
.
RequiredFieldValidator1.IsValid = false;
You shouldn't need to bother with the visibility, this will happen automatically.