| ASP.NET TextBox MaxLength in Multiline-Mode |
| Section: ASP.NET | Rating: Not rated yet! |
 | CodeGod submitted this resource The member's homepage is http://www.codegod.de Visit the profile here |
The only thing you need to do is to register a ClientScriptBlock in the Page_Load-eventhandler of the ASPX where you use your multiline TextBox:
const int LENGTH_TEXT = 100;
protected void Page_Load(object sender, EventArgs e)
{
string
lengthFunction = "function isMaxLength(txtBox) {";
lengthFunction += " if(txtBox) { ";
lengthFunction += " return ( txtBox.value.length <=" + LENGTH_TEXT + ");";
lengthFunction += " }";
lengthFunction += "}";
this.txtMyTextBox.Attributes.Add("onkeypress", "return isMaxLength(this);");
ClientScript.RegisterClientScriptBlock(
this.GetType(),
"txtLength",
lengthFunction , true);
}
If the length of the TextBox named txtMyTextBox exceeds the value of LENGTH_TEXT, the function isMaxLength returns false and there is no keypress possible anymore. Oh, I loves workarounds:-)