• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 47
  • Last Modified:

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:
        function AutoExpand(txtbox) {
            txtbox.style.height = "1px";
            txtbox.style.height = (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

  • 2
1 Solution
Ryan ChongCommented:
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 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
    End Sub

Open in new window

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.
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now