Resizing a textbox when populated with text - vb

GileadIT used Ask the Experts™
I am trying to resize textboxes on an 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:
        function AutoExpand(txtbox) {
   = "1px";
   = (25 + txtbox.scrollHeight) + "px";

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

Open in new window

Watch Question

Do more with

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

you may need to call the function AutoExpand when ur page is loading...
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 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
    End Sub

Open in new window


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