Search code examples
asp.netcsstextboxformattingrequiredfieldvalidator

asp:RequiredFieldValidator causing extra line in aspx page


I have simple ASPX page with few text buttons/drop downs. For almost every text button/drop down, I have the validators.

Issue I am facing is each of these validators produce a blank line on the page. Below is sample of the code in ASPX page. I tried with adding DISPLAY=DYNAMIC but I see blank lines getting reduced but when the validator message (or required * mark) is shown it shows that is not relevant near the textboxes.

            <tr>
            <asp:RequiredFieldValidator runat="server" ID="CityValidator" meta:resourceKey="CityValidator"
                ValidationGroup='<%# ValidationGroupName %>'  ControlToValidate="CityTextBox">*</asp:RequiredFieldValidator>
            <td>
                    <asp:TextBox runat="server" ID="CityTextBox" MaxLength='30' CssClass="TextBox" />
            </td>
                    <asp:RequiredFieldValidator runat="server" ID="RegionCodeValidator" meta:resourceKey="RegionCodeValidator"
                        ValidationGroup='<%# ValidationGroupName %>' ControlToValidate="RegionCodeTextBox" >*</asp:RequiredFieldValidator>
            <td>
                    <asp:TextBox runat="server" ID="RegionCodeTextBox" MaxLength='2' CssClass="TextBox"/>
            </td>
        </tr>

Solution

  • Set the validator display as dynamic. You should also put the validators within the table structure to keep your HTML structure valid.

       <tr>
            <td>
                    <asp:RequiredFieldValidator runat="server" ID="CityValidator" meta:resourceKey="CityValidator" Display="Dynamic" ValidationGroup='<%# ValidationGroupName %>'  ControlToValidate="CityTextBox">*</asp:RequiredFieldValidator>
                    <asp:TextBox runat="server" ID="CityTextBox" MaxLength='30' CssClass="TextBox" />
            </td>
            <td>
                    <asp:RequiredFieldValidator runat="server" ID="RegionCodeValidator" meta:resourceKey="RegionCodeValidator" Display="Dynamic" ValidationGroup='<%# ValidationGroupName %>' ControlToValidate="RegionCodeTextBox" >*</asp:RequiredFieldValidator>
                    <asp:TextBox runat="server" ID="RegionCodeTextBox" MaxLength='2' CssClass="TextBox"/>
            </td>
        </tr>