Hide unnecessary space when validation error messages are not firing in ASP page

devan picture devan · May 12, 2012 · Viewed 23.1k times · Source

I want to validate some text boxes in my ASP.NET page using ASP required field validation. And I want to display that error message in top of the page.

<table>
    <tr><td colspan='2'><b> User Input</b><br/></td></tr>
    <tr><td colspan='2'>
            <%--input validations--%>
            <asp:RegularExpressionValidator ID="regexpName1" runat="server"     
                ErrorMessage="This expression does not validate." 
                ControlToValidate="TextBox_adTitle"     
                ValidationExpression="^[a-zA-Z'.\s]{1,40}$" />
            <br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ControlToValidate="TextBox_1" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
        <br />
        </td>
    </tr>
    <tr><td>
        <asp:Label ID="Label_name" runat="server" Text="Seller Name * "></asp:Label>
        </td>
        <td>
             <asp:TextBox ID="TextBox_1" runat="server" ReadOnly="True" ></asp:TextBox>        
        </td>
    </tr>

...

This is working fine. However, the first table row is keeping its space even error messages are not displaying on it. This will cause the UI to look bad on the page since unnecessary space is there when the page loads.

How can I hide the space of the first row (validation's error messages row) while the page is loading and when there is no validation error?

Answer

Johnny_D picture Johnny_D · May 12, 2012

You need to set

Display="Dynamic"

property to your validator, this will cause desirable behaviour.