Resizing a textbox when populated with text - asp.net vb

GileadIT
GileadIT used Ask the Experts™
on
I am trying to resize textboxes on an asp.net page. I have successfully implemented a script to expand the textboxes as a user types (see below), but when the form is populated with text from a database, the size of the textbox is not expanded. Does anyone have an idea on how I might be able to remedy this?   Thanks - Joe

Expand textbox as user types:
<head>
<script>
        function AutoExpand(txtbox) {
            txtbox.style.height = "1px";
            txtbox.style.height = (25 + txtbox.scrollHeight) + "px";
        }
    </script>
</head>

<asp:TextBox ID="txtEvent" runat="server" CssClass="FormsTextbox" Rows="3" TextMode="MultiLine" style="overflow:hidden" onkeydown="AutoExpand(this)" ></asp:TextBox>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
you may need to call the function AutoExpand when ur page is loading...
Commented:
I ended up making a small subroutine to do the lifting... It works well, a bit more involved than I wanted but it works. For the javascript I posted, adding that into the page load was getting complicated so I dropped it. I'm better with vb.net than javascript.

Public Sub txtboxresize(ByVal txtboxes() As Control)
        Dim txtrows As Integer = 0
        Dim txtcolumns As Integer = 54

        For Each item In txtboxes
            'Check if field is a textbox
            If TypeOf item Is TextBox Then
                Dim txtBox As TextBox = item
                txtrows = (txtBox.Text.Length / txtcolumns) + 1
                If txtrows = 0 Or txtrows = 1 Then
                    txtrows = 3
                End If
                txtBox.Rows = txtrows
            End If
        Next
    End Sub

Open in new window

Author

Commented:
Although the reply to add the javascript to my page load would have worked, the work to be done on the script itself to make it work is not something I can do. Making a subroutine in vb to handle to work seemed easier.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial