Resizing a textbox when populated with text - vb

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongSoftware Team LeadCommented:
you may need to call the function AutoExpand when ur page is loading...
GileadITAuthor 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 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GileadITAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.