Limiter la taille de la textbox d'un boundfield

[code=c#]
public class LimitedBoundField : BoundField
{
private int _characterLimit = 0;
public int CharacterLimit
{
get { return _characterLimit; }
set { _characterLimit = value; }
}
public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
base.InitializeCell(cell, cellType, rowState, rowIndex);
if (rowState == DataControlRowState.Edit && cellType == DataControlCellType.DataCell)
{
TextBox tb = ((TextBox)cell.Controls[0]);
tb.MaxLength = _characterLimit;
tb.Text = tb.Text.Substring(0, (tb.Text.Length > _characterLimit) ? _characterLimit : tb.Text.Length);
}
}
} [/code]

Leave a Reply

Your email address will not be published. Required fields are marked *