Web- und Software Development

Das MaxLength ASP.NET TextBox Problem

Written By: Mario Priebe - Mrz• 17•09

Die TextBox unter ASP.NET beachtet leider nicht die MaxLength wenn man ihr die MultiRow-Eigenschaft mitgibt. Folgender Snippet behebt das Problem:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#region HelperMethod MaxLength
/// <summary>
/// Eine TextBox welche die MultiRow Eigenschaft besitzt, ignoriert die MaxLength (bug)
/// Diese Methode (JavaScript) behebt das Problem.
/// Quelle: http://www.codegod.de/WebAppCodeGod/aspnet-textbox-maxlength-in-multiline-mode-AID297.aspx
/// </summary>
const int maxLengthTextBox = 1000;
protected void MaxLength()
{
    String
    lengthFunction = "function isMaxLength(txtBox){";
    lengthFunction += " if(txtBox) { ";
    lengthFunction += "   return (txtBox.value.length <" + maxLengthTextBox + ");";
    lengthFunction += " }";
    lengthFunction += "}";
 
    txtBox1.Attributes.Add("onkeypress", "return isMaxLength(this);");
    txtBox2.Attributes.Add("onkeypress", "return isMaxLength(this);");
    Page.ClientScript.RegisterClientScriptBlock(GetType(), "txtLength", lengthFunction, true);
}
#endregion


Quellenangabe: codegod

Ähnliche Beiträge

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.