繁体   English   中英

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

[英]Dynamically resize the Multiline textbox in VB.Net

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

代码:

<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';
        }
    }

任何帮助将不胜感激。

嗨,我找到了解决我的问题的方法。 请在 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

您可以使用这个 jQuery 插件: autogrow-textarea-plugin

问题是您必须处理一些在不同浏览器中不同的事件(fe onpropertychange),以便文本区域在加载或按键时更改其高度。 jQuery 插件使它更容易。

基本上你的问题是你的代码背后没有“意识到”高度的变化。 有几种方法可以解决这个问题。

一种方法是将大小值写入从 onbeforeunload 调用的隐藏字段,然后从隐藏字段 onload 中读取。

暂无
暂无

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

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