Set databound repeater textbox height at runtime based on text length

In an ASP.Net 3.5 website we have a databound repeater in one of the tabs of an AJAXToolkit TabContainer.

We would like to adjust the height of one of the repeater's textboxes at runtime based on the length of the text it contains.

We have tried a very crude height adjustment in the repeater's ItemDataBound event in code-behind based on text length but we would like something much more accurate.

Can any of the Experts here help with this please?
Who is Participating?
Al230762Connect With a Mentor Author Commented:
For future reference, this is how we did it:

<asp:TextBox ID="txtEntryLevel" runat="server" Font-Size="11px" Height="60px"
                                                                        Text='<%# Container.DataItem("EntryLev")%>' TextMode="MultiLine" ToolTip="255 characters limit"
                                                                        Width="140px" CssClass="normalText" onkeypress="return textboxMultilineMaxNumber(this,255)" onclick="setHeight(this);" onkeydown="setHeight(this);" onfocus="setHeight(this);"></asp:TextBox>

<script type="text/javascript">
    function setHeight(txtdesc) {
     var boxHeight = parseInt(, 10);
     var textHeight = txtdesc.scrollHeight;

     if (txtdesc.value !== "" && boxHeight < textHeight)
   = txtdesc.scrollHeight + "px";
Alexandre SimõesManager / Technology SpecialistCommented:
Is it a Textbox or a TextArea?

From what I understand you want to always show all text and expand vertically to accomplish that. right?

Don't know if you're using jQuery already but this plugin might help you:

Even if you don't use jQuery the code is pretty straight forward.
What he does is use an hidden div and set its width the same as the textarea and apply the same text inside. What this does is that the div will resize itself to fit all the text.
This calculation is done by the browser rendering engine so after you just need to get the div height and apply it to your textarea.

It's ingenious but it works :)

Ah... and using the jQuery plugin will make it easier for you to centralize the code.
As you might have multiple textarea elements, you'll need an hidden div for each one of them...
Al230762Author Commented:
Now why didn't I find that link when searching the web!? Thank you Alexandre that does indeed look ingenious. We'll work on that and let you know how it goes.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Al230762Author Commented:
Alexandre - we are actually using databound textboxes in a repeater rather than textareas. However we have changed the textbox to textarea and implemented the jQuery you suggested but it seems that the textarea only expands when the user clicks inside it.

Maybe I've misunderstood but it seems that is what the jQuery is designed to do i.e. expand onkeyup.

What we need is for the textbox/textarea to autosize when the page is first loaded.

Any thoughts on that?
Alexandre SimõesManager / Technology SpecialistCommented:
Hi mate,
I'm really sorry I lost track of this one.

I assembled a custom implementation of the hack above:

It should be enough to show you what I meant.
To make this work you just need to set the custom attribute autoHeight to the textareas you want to autosize and call applyAutoSize(); at the end of the body or on document.ready

<textarea id="txt1" autoHeight></textarea>
<textarea autoHeight>With initial text</textarea>

Open in new window

var applyAutoSize = function(){
    var $hDiv;
    var resize = function(){
        var $this= $(this),
            width = $this.width(),
            text= $this.val(),
        // prevent really thin textareas
        if(text === "") 
            modifText= "|";
            modifText = text;
        // replace CR with <br/>
        modifText = modifText.replace(/(\r\n|\n|\r)/g,"<br/>");
        // setup the hidden div
            .css('font-family', $this.css('font-family'))
        // pick the hidden div height and set it to the textarea
    var init = function(){
        var $txt = $('textarea[autoHeight]');
        // dynamically add the hidden div
        $hDiv = $('div#__autoHeight');
        if($hDiv.length === 0){
            $hDiv = $('<div id="__autoHeight" style="display:none;word-wrap: break-word;padding: 3px;"></div>');
        $txt.css('overflow', 'hidden');
        // capture all events
        $txt.on('change keyup keydown paste', function(){


Open in new window

Again, sorry for the delay.

Al230762Author Commented:
We resolved the solution ourselves.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.