Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Category(All)]
       [StringLength(40)]
       [DisplayName(ITEMNAME)]
       [XmlAttribute]
       public override string Label
       {
           get
           {
               return base.Label;
           }
           set
           {

               value = value.TrimEnd();   ignorieren


               if (value.length > 40)
               {
                //here I want a user not to allow to write more than 40 char
                  when user write something in  Name feild max Length 40 i want a user to allow

               }


           }
       }

This is my code for Property grid view for ITEMNAME i want that value should not be longer han 40 , max value is 40 so what i want to do is that when user enter a value grater than 40 want to make property feild disabled so that user can#t enter more.
After researching i found a stringLength attribute(System.ComponentModel.DataAnnotations) but its seems to be not working anyone help me in this or thete is another way to solve This ???????????
Posted
Updated 5-Sep-14 4:01am
v3

it's mvc or standard web forms ?

System.ComponentModel.DataAnnotations is used with MVC
ex :
C#
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
XML
<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
CSS
.truncate {
  width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
 
Share this answer
 
its for Windows Form Application i am working with .Net 3.5 but its not supporting MaxLength attribute i have added .dll and using System.ComponentModel.DataAnnotations
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900