Tags

,

For a lot of developers, web forms technology is not the first choice. However, if you are required to work with web forms, you will probably work with aspx validators. And one of the problems is to wrap error message for the validator. Solution is very simple, even is not straight forward.

We assume we have this control and we set the size to 200px.

<asp:TextBox runat="server" Width="200px" ID="RequiredField" />

If we place below a required field validator with Display property set to Dynamic and we also set a very long error message, it will appear of course longer than 200px and it might affect the layout. To bypass this problem, we can wrap the validator into a div.

        <div style="display:block; width:200px; white-space:normal">
            <asp:RequiredFieldValidator runat="server" ErrorMessage="A text longer than 200px" ControlToValidate="RequiredField" Display="Dynamic"  />
        </div>

I know is not perfect solution, but is simple and is doing the job.

Advertisements