简体   繁体   English

在 VB.Net 中动态调整多行文本框的大小

[英]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.我正在 VB.net 中的一个网站上工作,我需要创建一个多行文本框。 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.随着使用 Javascript 字符数的增加,我可以增加文本框的高度,但是当页面刷新或再次加载时,文本框大小会恢复到默认高度。

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:请在 txtDescription_Load 事件中写入以下代码:

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 .您可以使用这个 jQuery 插件: 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.问题是您必须处理一些在不同浏览器中不同的事件(fe onpropertychange),以便文本区域在加载或按键时更改其高度。 The jQuery Plugin makes it easier. jQuery 插件使它更容易。

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.一种方法是将大小值写入从 onbeforeunload 调用的隐藏字段,然后从隐藏字段 onload 中读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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