Setting amount of text allowed in textbox during detailsview edit or insert

Last post 06-04-2009 3:48 AM by sjnaughton. 9 replies.

Sort Posts:

  • Setting amount of text allowed in textbox during detailsview edit or insert

    04-07-2009, 11:36 PM
    • Member
      13 point Member
    • mdesoysa
    • Member since 12-03-2008, 9:04 PM
    • Posts 60

    Hi there,

     Is there any way to set the amount  text a user can into the textbox during edit/insert mode of a detailsview? Right now I have insert and edit validation but it still allows users to enter more text (it just shows an error message and the user needs to reduce the amount of text)

    The method I've implemented can get tedious for the user as they would have to count the number of letter entered and then reduce to the right amount.

    Is there an easier way?

     

    Cheers.
    Mel
  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    04-08-2009, 3:00 AM
    • Contributor
      2,060 point Contributor
    • andreadottor
    • Member since 01-25-2007, 10:21 AM
    • Italy
    • Posts 312

    For limit the value in insert and edit you can use the rangeattribute that you can specify the min and max value that you accept.
    http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx

    [Range(10, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    public object Weight;

    Or if you want limit a lenght of a string there is a StringLengthAttribute
    http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx

     [StringLength(10, ErrorMessage= "The file name cannot exceed 10 characters long")]
    public object ProductNumber;

     Or if you want other validation you can use a RegularExpressionAttribute
    http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.regularexpressionattribute.aspx

    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]
    public object LastName;

     

    If this answers your question, please select 'mark as answer' thanks!



    Andrea Dottor
    Microsoft MVP - ASP/ASP.NET
  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    04-08-2009, 5:16 AM
    Answer
    • Star
      12,372 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,574
    • TrustedFriends-MVPs

    Hi Mdesoysa, if you look at the code in the Text_Edit field template Page_Load method: 

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.MaxLength = Column.MaxLength;
        if (Column.MaxLength < 20)
            TextBox1.Columns = Column.MaxLength;
        TextBox1.ToolTip = Column.Description;
    
        SetUpValidator(RequiredFieldValidator1);
        SetUpValidator(RegularExpressionValidator1);
        SetUpValidator(DynamicValidator1);
    }
    You will see the line in BOLD ITALIC if your column size is set to say 10 the text box length will be set to 10 and and so you will not be able to enter more than 10 chars. But as you can see from the code this is turned off after 20 chars so what you could do is: 
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.MaxLength = Column.MaxLength;
        if (Column.MaxLength < 20)
        {
            TextBox1.Columns = Column.MaxLength;
        }
        else
        {
            TextBox1.Columns = Column.MaxLength;
            TextBox1.Width = 150;
        }
    
        TextBox1.ToolTip = Column.Description;
    
        SetUpValidator(RequiredFieldValidator1);
        SetUpValidator(RegularExpressionValidator1);
        SetUpValidator(DynamicValidator1);
    }
     What happens now is the columns property of the textbox is now always set but the width of the textbox is now fixed at over 20 chars.
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    06-03-2009, 12:23 AM
    • Member
      13 point Member
    • mdesoysa
    • Member since 12-03-2008, 9:04 PM
    • Posts 60

     Hi Sjnaughton,

     So if want to set different textbox widths for each column does that mean I would have to create a new Field template for each one? or can I create a new class for each one that inherits the Text_Edit field template?

    Both these ways still seem quite tedious especially if you have a lot of columns with different textbox lengths required...

    Please let me know if I'm on the right track?

    Cheers.
    Mel
  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    06-03-2009, 5:23 AM
    • Star
      12,372 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,574
    • TrustedFriends-MVPs

    no you could add an Attribute to set the column width and then in the Template check the length and set it there.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    06-03-2009, 10:53 AM
    • Participant
      785 point Participant
    • DaveRuss
    • Member since 02-16-2009, 9:24 AM
    • Cambridgeshire
    • Posts 169

    This is a bugbear of mine with DD.

    I don't want to set the field-lengths of my strings in metadata AND the database, I want the MaxLength to automatically inherit the database maxlength.

    If I want to set a MINimum length I will use the attribute, but I CAN'T SET A MINLENGTH WITHOUT SETTING MAXLENGTH AS WELL! Grrrrrr.....If I set a maxlength of 0 when I specify a minlength as well I get an error......

    I ended up writing my own extension method MaxLength2 which retrieves maxlength from the database for the applicable field.  Wherever the templates used MaxLength, I made them use my MaxLength2 instead.  I still have to specify a maxlength when I want a minlength though.....

     

  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    06-03-2009, 11:54 AM
    • Star
      12,372 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,574
    • TrustedFriends-MVPs

    The rant seems pointless as you can easily set this up, the reason this does not happen out of the box is most want the defaults.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    06-03-2009, 12:35 PM
    Answer
    • Star
      12,372 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,574
    • TrustedFriends-MVPs

    For those odd Fields that are not caught by MultilineText you could try something like this:

     

    private int LargeFiled;
    protected void Page_Load(object sender, EventArgs e)
    {
        // get larg field min size default = 100
        LargeFiled = Session["LargeField"] != null ? int.Parse(Session["LargeField"].ToString()) : 100;
    
        TextBox1.MaxLength = Column.MaxLength;
        if (Column.MaxLength < 20)
        {
            TextBox1.Columns = Column.MaxLength;
        }
        else if(Column.MaxLength >= LargeFiled)
        {
            TextBox1.TextMode = TextBoxMode.MultiLine;
            TextBox1.Width = 100;
            TextBox1.Height = 150;
        }
    
        TextBox1.ToolTip = Column.Description;
    
        SetUpValidator(RequiredFieldValidator1);
        SetUpValidator(RegularExpressionValidator1);
        SetUpValidator(DynamicValidator1);
    }
     in the Text_Edit FieldTemplate
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    06-04-2009, 3:26 AM
    • Participant
      785 point Participant
    • DaveRuss
    • Member since 02-16-2009, 9:24 AM
    • Cambridgeshire
    • Posts 169

     I disagree with you that "most people" want to set their field lengths in both the database AND the metadata.

    Yes, it's fairly easy to setup your own implementation of MaxLength, but is there ever a time when I want a DB field to allow 20 characters, but the screen field to only allow 10?  The model always has a maxlength for a text column, so why not just use it "by default" and have the attribute used to override it, if you want to?

    I realise it's a preference thing, but my preference is to have to do as little work as possible :)

     

    Dave

  • Re: Setting amount of text allowed in textbox during detailsview edit or insert

    06-04-2009, 3:48 AM
    • Star
      12,372 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,574
    • TrustedFriends-MVPs

    Hi Dave, I agree with you (about doing as little as possibleBig Smile) I can't see why you would have two different sizes? All I'm interested in is fixing the width of the textbox to a sensible size after all you don't what your textbox to be 200 chars wide do you.

    The nice thing about DD is how extensible it is (without the need to add attributes) after all nobody is going to please everybody all of the time are they, so I'm glad the ASP.Net team made DD so extensible Big Smile

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
Page 1 of 1 (10 items)