简体   繁体   中英

Dynamically resize the Multiline textbox in VB.Net

I am working on a site it is in VB.net, I need to create a multiline text box. I am able to increase the height of the textbox as number of characters increases using Javascript, however when page get refreshed or loaded again then textbox size comes back to the default height.

Code:

<asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" rows="3" onkeypress="grow();" Width="590px"></asp:TextBox>

    var TEXTAREA_LINE_HEIGHT = 2;
    function grow(source) 
    {
        var textarea = document.getElementById(source);
        var newHeight = textarea.scrollHeight;
        var currentHeight = textarea.clientHeight;

        if (newHeight > currentHeight) {
            textarea.style.height = newHeight + 5 * TEXTAREA_LINE_HEIGHT + 'px';
        }
    }

Any help would be highly appreciated.

Hi I found out the solution for my problem. Please write the following code on txtDescription_Load event:

Protected Sub txtDescriptiont_Load(ByVal sender As Object, ByVal e As System.EventArgs)     Handles txtDescription.Load
    Dim charRows As Integer = 0
    Dim tbCOntent As String
    Dim chars As Integer = 0
    tbCOntent = txtInput.Text
    txtInput.Columns = 25
    chars = tbCOntent.Length
    charRows = chars / txtInput.Columns
    Dim remaining As Integer = chars - charRows * txtDescription.Columns
    If remaining = 0 Then
        txtDescription.Rows = charRows
        txtDescription.TextMode = TextBoxMode.MultiLine
    Else
        txtDescription.Rows = charRows
        txtDescription.TextMode = TextBoxMode.MultiLine
    End If
End Sub

You can use this jQuery Plugin: autogrow-textarea-plugin .

The problem is that you must handle some events that are different in different browsers(fe onpropertychange), so that the textarea changes it's height on load or on keypress. The jQuery Plugin makes it easier.

Basically your problem is your code behind isn't "aware" of the changes to the height. There are a couple of ways to solve this.

One way is to write the size value to a hidden field called from onbeforeunload and then read from the hidden field onload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM