it's mvc or standard web forms ?
System.ComponentModel.DataAnnotations is used with MVC
ex :
public class User
{
[Required(ErrorMessage = "Name is required.")]
[MaxLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")]
public string Name { get; set; }
}
with web forms you can use regex validator
<asp:RegularExpressionValidator runat="server" ID="valInput"
ControlToValidate="txtInput"
ValidationExpression="^[\s\S]{0,40}$"
ErrorMessage="Please enter a maximum of 40characters"
Display="Dynamic">*</asp:RegularExpressionValidator>
if it is only the display that you need to truncate you can use css and ellipsis
.truncate {
width: 250px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}