【问题标题】:How to display remaining textbox characters in a label in C#?如何在 C# 的标签中显示剩余的文本框字符?
【发布时间】:2013-06-04 00:31:39
【问题描述】:

我不确定我是否有最后一点,对吗?我将文本框的最大长度更改为 140。我似乎无法使用 TextLength。请帮忙?!到目前为止我有这个:

protected void textBox_TextChanged(object sender, EventArgs e)
{
    characterCountLabel.Text = textBox.MaxLength - textBox.TextLength;
}

【问题讨论】:

  • 您是否遇到错误?...计数错误?...或者您的标签永远不会更新?如果是最后一个,请确保您已将 TextChanged() 事件或您的 TextBox 连接到此方法。
  • 我在TextLength下得到一条红色波浪线
  • 您的应用程序使用 WinForms 还是 WPF?您可以使用(无论如何这不是最好的方法)String.LengthtextBox.Text 的属性。
  • 我正在使用visual web developer 2010. ASP.net
  • ASP.NET WebForms?好吧,尝试使用textBox.Text.Length

标签: c# textbox character


【解决方案1】:

characterCountLabel.Text 是字符串格式。因此,您可能希望在设置其值之前对其进行转换:

protected void textBox_TextChanged(object sender, EventArgs e)
{
    characterCountLabel.Text = (textBox.MaxLength - textBox.Text.Length).ToString();
}

我认为您正在尝试显示用户可以输入到您的文本框中的剩余字符?我建议您可以像这样将限制设置为常量:

protected void textBox_TextChanged(object sender, EventArgs e)
    {
        characterCountLabel.Text = (140 - textBox.Text.Length).ToString(); // in here 140 is your limit
    }

如果您在 C# 中使用 ASP.NET。不要限制自己使用 javascript like in this link

【讨论】:

  • 您好,感谢您的回答。我的标签没有显示任何内容。
  • 什么意思。你在使用 WebForms 吗?还是 WinForms?
  • 我正在尝试计算剩余的活动角色。例如,在推特上。当您输入推文时,剩余的字符会在您输入时显示。我能做到吗?
  • 是的。当然,但我强烈建议您使用 javascript 而不是 textBox_textChanged 方法。让我更新我的答案。
【解决方案2】:

我认为这将是一个更好的答案...

标题代码:

<script language="javascript" type="text/javascript">
    function getCountDown() 
    {
        //Get the Textbox control
        var textField = document.getElementById("<%#TextBox1.ClientID %>");
        //Do the math of chars left and pass the value to the label
        document.getElementById('<%#Label1.ClientID %>').innerHTML = textField.maxLength - textField.value.length;
        return false;
    }        
</script>

ASP 代码:

<asp:TextBox ID="TextBox1" runat="server" MaxLength="729" Height="80px" 
                Width="591px" onkeyup="getCountDown();" ClientIDMode="Static"></asp:TextBox>

<asp:Label ID="Label1" runat="server" Text="" ClientIDMode="Static"></asp:Label>

TextBoxLabel控件的属性设置为ClientIDMode="Static"很重要,否则在javascript上将找不到控件名称。

CS 代码:

protected void Page_Load(object sender, EventArgs e)
{
    Page.Header.DataBind();
}

SingleLine TextBox 就是这样。

现在,对于 MultiLine TextBox,您需要将其添加到您的 Page_Load(),以便 maxLength 采用 TextBox1.MaxLength 值。:

this.TextBox1.Attributes.Add("maxLength", TextBox1.MaxLength.ToString());

此外,TextBoxMaxLength 属性在 Multiline 模式下也不起作用,因此您需要在 javascript getCountDown() 函数中添加以下行:

// Check if user is entering more than the limit of characters
if (textField.value.length >= textField.maxLength) {
    // Cut extra text
    document.getElementById("<%#TextBox1.ClientID %>").innerHTML = textField.value.substring(0, textField.maxLength);
}

var textField = document.getElementById("&lt;%#TextBox1.ClientID %&gt;"); 行之后添加它们。这是为了防止用户输入比MaxLength 值更多的字符。

巴勃罗

【讨论】:

  • 最佳答案,最佳!@!@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多