Wrap error message for a web form validator

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"  />

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


Author: anvlpopescu

Nothing special to say. I'll think about it more and let your know.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s