.Net bug when adding ‘MaxLength’ attribute to MultiLine Textboxes

Another little hidden feature within .Net for you!

.Net kindly ignores the ‘MaxLength’ attribute for a textbox with it’s ‘TextMode’ set to ‘MultiLine’, if it is set within the frontend files.

Woop! Why I’m not sure, but the simple workaround is to do this within the backend. Personally I choose to do it within the PreRender method.

<!-- Omits MaxLength Attribute -->
<asp:TextBox ID="Textbox1" Runat="server" TextMode="MultiLine" MaxLength="500" />

<!-- Fixed via adding MaxLength attribute in Backend code -->
<asp:TextBox ID="Textbox1" Runat="server" TextMode="MultiLine" />
// Add MaxLength attribute via code
Textbox1.Attribute.Add("maxlength", "500");

That’s all folks! Keeping it nice and simple ;)

4 thoughts on “.Net bug when adding ‘MaxLength’ attribute to MultiLine Textboxes

  1. I’m utilizing this workaround, which does fine for Chrome and Firefox. However, IE9 still ignores the MaxLength attribute even if set in the code-behind.

    • I want to be surprised at this, but completely fail to.

      I will have a little play over the next week to see if I can encourage IE to behave :)

    • @Bob

      I’ve just checked in IE11, and have run IE11 in IE9 mode, both seems to accept the maxlength attribute to a textarea.

      When you view source on the rendered page, can you see ‘maxlength’ within the textarea, or has it been omitted? Does this same page work for you chrome or firefox?

      If you want you can email me the source test aspx page, and I can see if I can spot what’s happening. (reply and I’ll email you direct)

  2. I tried this and I see MaxLength being rendered in the markup but it has no effect in IE11 or Chrome for me.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>