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

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

LVL 1
GileadITAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you may need to call the function AutoExpand when ur page is loading...
0
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 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

0

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.
0
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
ASP.NET

From novice to tech pro — start learning today.