如何在C#中显示标签中的剩余文本框字符?

Kir*_*udd 2 c# textbox character

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

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

Chr*_*ark 5

characterCountLabel.Text是字符串格式.所以你可能想要在设置它的值之前转换它:

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

我想您正在尝试显示用户可以输入到文本框的剩余字符?我建议您可以将限制设置为常量,如下所示:

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

如果您在C#中使用ASP.NET.不要限制自己使用此链接中的 javascript