Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

Creation of Custom Text Control with MaxLength Of 10 Characters

Vote:
 

Hi all,

I am facing problem when creaing custom String property which MaxLength should only be 10. Even i run the project and Adding a property to template i could enter more than 10 characters. What may be the problem? Please look into the below code and tell me whether it is correct way or not. I have added the CustomControl in Models Folder.

TextPropertyCustom.cs

using System;
using System.Collections.Generic;
using System.Text;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.PlugIn;
using EPiServer.Web.PropertyControls;

namespace MyDemo1.Models
{
    /// <summary>
    /// Custom PropertyData implementation
    /// </summary>
    [Serializable]
    [PropertyDefinitionTypePlugIn]
    public class TextPropertyCustom : EPiServer.Core.PropertyString
    {
        public override int MaxLength
        {
            get
            {
                return base.MaxLength;
            }
            set
            {
                base.MaxLength = 10;
            }
        }

        public override IPropertyControl CreatePropertyControl()
        {
            return new TextPropertyCustomControl();
        }

    }
}

    

  TextPropertyCustomControl.cs

 

using System;
using System.Collections.Generic;
using System.Text;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Web.PropertyControls;
using EPiServer.Web.WebControls;

namespace MyDemo1.Models
{
    /// <summary>
    /// PropertyControl implementation used for rendering TextPropertyCustom data.
    /// </summary>
    public class TextPropertyCustomControl : EPiServer.Web.PropertyControls.PropertyStringControl
    {
        public TextPropertyCustom TextPropertyCustom
        {
            get
            {
                return PropertyData as TextPropertyCustom;
            }
        }
    }
}

And one more query, Where i can exactly create this custom controls? Whether in Models folder or Models - Pages Folder?

#81634
Feb 21, 2014 10:18
Vote:
 

You do not need a custom property for this, just do like this:

[Display(
        Name = "StringLenghAttribute",
        Description = "StringLenghAttribute.",
        GroupName = "Validation tests",
        Order = 101)]
[StringLength(10)]
public virtual string StringLenghAttribute { get; set; }

David explains the validation attributes here:

http://www.david-tec.com/2012/06/EPiServer-7-Preview---Using-validation-attributes/

#121047
Apr 30, 2015 11:51
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.