|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
MAXLENGTH SUR LES TEXTBOX MULTILINE
Information sur la source
Description
J'avais besoin de limiter le nombre de caractères saisis dans ces zones de texte pour l'application qu'on m'a donné à maintenir à mon Taf... J'ai eu la surprise de voir que MaxLength ne fonctionnait pas si le TextBox était "Multiline". Ma connexion à Internet au bureau étant inexistante, j'ai écumé le net toute la soirée (merci GG) à la recherche de LA solution. (cette précision est pour revendiquer ce code source comme étant le mien et pouvoir vous l'offrir, désolé si je suis rude il est 1h30 du mat') Je n'ai trouvé que des solutions partielles qui ne me convenaient pas, j'ai donc compilé toutes les techniques en une pour avoir "THE Ultimate Solution". Utilisation très simple : copier coller la definition de la variable MLTextBoxCount, la fonction PatchMLTextBox et le contenu de mon PageLoad à placer au début du vôtre... Et hop ! Mâââgique !!! vos TextBox Multiline reconnaitront la valeur mise dans la propriété MaxLength...
Source
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
-
- public partial class _Default : System.Web.UI.Page
- {
- static protected int MLTextBoxCount = 0;
-
- protected void PatchMLTextBox(Control parent)
- {
-
- foreach (Control c in parent.Controls)
- {
- if (c.HasControls()) PatchMLTextBox(c);
- if (c is TextBox)
- {
- if (((TextBox)c).TextMode == TextBoxMode.MultiLine)
- {
- if (((TextBox)c).MaxLength > 0)
- {
- MLTextBoxCount++;
- // Ajout en dur de la propriété MaxLength car ASP.NET le vire dans ce cas là
- ((TextBox)c).Attributes.Add("MaxLength", ((TextBox)c).MaxLength.ToString());
- // Ajout des evenements à intercepter pour verifier les longueurs
- ((TextBox)c).Attributes.Add("onkeypress", "return verifyMaxLengthOnKey(this);");
- ((TextBox)c).Attributes.Add("onpaste", "return verifyMaxLengthOnPaste(this);");
- ((TextBox)c).Attributes.Add("ondrop", "return verifyMaxLengthOnDragDrop(this);");
- }
- }
- }
-
- }
- }
-
- protected void Page_Load(object sender, EventArgs e)
- {
- MLTextBoxCount = 0;
- PatchMLTextBox(this);
- if (MLTextBoxCount > 0)
- {
- // Si c'est utile on ajoute le corps des fonctions Javascript
-
- ClientScript.RegisterClientScriptBlock(this.GetType(), "txtVarAlert", "var ShowAlertIfMaxLength = true;", true);
-
- String OnKeyFunction = "function verifyMaxLengthOnKey(ref) {\n";
- OnKeyFunction += " var SelText;\n";
- OnKeyFunction += " var Range;\n";
- OnKeyFunction += " if (ref.MaxLength != null) {\n";
- OnKeyFunction += " Range = window.document.selection.createRange();\n";
- OnKeyFunction += " SelText = Range.text;\n";
- OnKeyFunction += " if (SelText != null && SelText != '' && SelText != undefined) {\n";
- OnKeyFunction += " return true;\n";
- OnKeyFunction += " }\n";
- OnKeyFunction += " if (ref.value.length >= ref.MaxLength) {\n";
- OnKeyFunction += " return false;\n";
- OnKeyFunction += " }\n";
- OnKeyFunction += " return true;\n";
- OnKeyFunction += " }\n";
- OnKeyFunction += "}\n";
- ClientScript.RegisterClientScriptBlock(this.GetType(), "txtOnKey", OnKeyFunction, true);
-
- String OnPasteFunction = "function verifyMaxLengthOnPaste(ref) {\n";
- OnPasteFunction += " var Data = window.clipboardData.getData('Text');\n";
- OnPasteFunction += " var SelText;\n";
- OnPasteFunction += " var Range; \n";
- OnPasteFunction += " var msg; \n";
- OnPasteFunction += "\n";
- OnPasteFunction += " if (ref.MaxLength != null) {\n";
- OnPasteFunction += " Range = window.document.selection.createRange();\n";
- OnPasteFunction += " SelText = Range.text; \n";
- OnPasteFunction += " if (SelText != null && SelText != '' && SelText != undefined) {\n";
- OnPasteFunction += " if ((ref.value.length - SelText.length) + Data.length < ref.MaxLength) {\n";
- OnPasteFunction += " return true; \n";
- OnPasteFunction += " }\n";
- OnPasteFunction += " }\n";
- OnPasteFunction += " if (ref.value.length + Data.length >= ref.MaxLength) {\n";
- OnPasteFunction += " if (ShowAlertIfMaxLength == true) {\n";
- OnPasteFunction += " msg = 'Le texte à coller ne peut être contenu ';\n";
- OnPasteFunction += " msg += 'dans cette zone de saisie \\n'; \n";
- OnPasteFunction += " msg += 'limitée à ' + ref.MaxLength + ' caractères ';\n";
- OnPasteFunction += " alert(msg); \n";
- OnPasteFunction += " }\n";
- OnPasteFunction += " // return false;\n";
- OnPasteFunction += " window.clipboardData.setData('Text',Data.substring(0,ref.MaxLength-ref.value.length));\n";
- OnPasteFunction += " }\n";
- OnPasteFunction += " return true;\n";
- OnPasteFunction += " }\n";
- OnPasteFunction += "}\n";
-
- ClientScript.RegisterClientScriptBlock(this.GetType(), "txtOnPaste", OnPasteFunction, true);
-
- String OnDropFunction = "function verifyMaxLengthOnDragDrop(ref) {\n";
- OnDropFunction += " var seltext;\n";
- OnDropFunction += " var range;\n";
- OnDropFunction += " var data;\n";
- OnDropFunction += " var msg;\n";
- OnDropFunction += "\n";
- OnDropFunction += " if (ref.MaxLength != null) {\n";
- OnDropFunction += " data = window.event.dataTransfer.getData('Text');\n";
- OnDropFunction += " range = window.document.selection.createRange();\n";
- OnDropFunction += " seltext = range.text;\n";
- OnDropFunction += " if (seltext != null && seltext != '' && seltext != undefined) {\n";
- OnDropFunction += " if ((ref.value.length - seltext.length) + data.length <= ref.MaxLength) {\n";
- OnDropFunction += " return true;\n";
- OnDropFunction += " }\n";
- OnDropFunction += " }\n";
- OnDropFunction += " if (ref.value.length + data.length > ref.MaxLength) {\n";
- OnDropFunction += " if (ShowAlertIfMaxLength == true) {\n";
- OnDropFunction += " msg = 'Le texte à déposer ne peut être contenu ';\n";
- OnDropFunction += " msg += 'dans cette zone de saisie \\n'; \n";
- OnDropFunction += " msg += 'limitée à ' + ref.MaxLength + ' caractères ';\n";
- OnDropFunction += " alert(msg);\n";
- OnDropFunction += " }\n";
- OnDropFunction += " return false; // Si la ligne suivante fonctionnait il faudrait virer celle là\n";
- OnDropFunction += " // window.event.dataTransfer.setData('Text',data.substring(0,ref.MaxLength-ref.value.length));\n";
- OnDropFunction += " }\n";
- OnDropFunction += " return true;\n";
- OnDropFunction += " }\n";
- OnDropFunction += "}\n";
-
- ClientScript.RegisterClientScriptBlock(this.GetType(), "txtOnDrop", OnDropFunction, true);
- }
-
- // ==================================================================
- // = Ici débute votre PageLoad
- // ==================================================================
-
-
- }
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
static protected int MLTextBoxCount = 0;
protected void PatchMLTextBox(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.HasControls()) PatchMLTextBox(c);
if (c is TextBox)
{
if (((TextBox)c).TextMode == TextBoxMode.MultiLine)
{
if (((TextBox)c).MaxLength > 0)
{
MLTextBoxCount++;
// Ajout en dur de la propriété MaxLength car ASP.NET le vire dans ce cas là
((TextBox)c).Attributes.Add("MaxLength", ((TextBox)c).MaxLength.ToString());
// Ajout des evenements à intercepter pour verifier les longueurs
((TextBox)c).Attributes.Add("onkeypress", "return verifyMaxLengthOnKey(this);");
((TextBox)c).Attributes.Add("onpaste", "return verifyMaxLengthOnPaste(this);");
((TextBox)c).Attributes.Add("ondrop", "return verifyMaxLengthOnDragDrop(this);");
}
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
MLTextBoxCount = 0;
PatchMLTextBox(this);
if (MLTextBoxCount > 0)
{
// Si c'est utile on ajoute le corps des fonctions Javascript
ClientScript.RegisterClientScriptBlock(this.GetType(), "txtVarAlert", "var ShowAlertIfMaxLength = true;", true);
String OnKeyFunction = "function verifyMaxLengthOnKey(ref) {\n";
OnKeyFunction += " var SelText;\n";
OnKeyFunction += " var Range;\n";
OnKeyFunction += " if (ref.MaxLength != null) {\n";
OnKeyFunction += " Range = window.document.selection.createRange();\n";
OnKeyFunction += " SelText = Range.text;\n";
OnKeyFunction += " if (SelText != null && SelText != '' && SelText != undefined) {\n";
OnKeyFunction += " return true;\n";
OnKeyFunction += " }\n";
OnKeyFunction += " if (ref.value.length >= ref.MaxLength) {\n";
OnKeyFunction += " return false;\n";
OnKeyFunction += " }\n";
OnKeyFunction += " return true;\n";
OnKeyFunction += " }\n";
OnKeyFunction += "}\n";
ClientScript.RegisterClientScriptBlock(this.GetType(), "txtOnKey", OnKeyFunction, true);
String OnPasteFunction = "function verifyMaxLengthOnPaste(ref) {\n";
OnPasteFunction += " var Data = window.clipboardData.getData('Text');\n";
OnPasteFunction += " var SelText;\n";
OnPasteFunction += " var Range; \n";
OnPasteFunction += " var msg; \n";
OnPasteFunction += "\n";
OnPasteFunction += " if (ref.MaxLength != null) {\n";
OnPasteFunction += " Range = window.document.selection.createRange();\n";
OnPasteFunction += " SelText = Range.text; \n";
OnPasteFunction += " if (SelText != null && SelText != '' && SelText != undefined) {\n";
OnPasteFunction += " if ((ref.value.length - SelText.length) + Data.length < ref.MaxLength) {\n";
OnPasteFunction += " return true; \n";
OnPasteFunction += " }\n";
OnPasteFunction += " }\n";
OnPasteFunction += " if (ref.value.length + Data.length >= ref.MaxLength) {\n";
OnPasteFunction += " if (ShowAlertIfMaxLength == true) {\n";
OnPasteFunction += " msg = 'Le texte à coller ne peut être contenu ';\n";
OnPasteFunction += " msg += 'dans cette zone de saisie \\n'; \n";
OnPasteFunction += " msg += 'limitée à ' + ref.MaxLength + ' caractères ';\n";
OnPasteFunction += " alert(msg); \n";
OnPasteFunction += " }\n";
OnPasteFunction += " // return false;\n";
OnPasteFunction += " window.clipboardData.setData('Text',Data.substring(0,ref.MaxLength-ref.value.length));\n";
OnPasteFunction += " }\n";
OnPasteFunction += " return true;\n";
OnPasteFunction += " }\n";
OnPasteFunction += "}\n";
ClientScript.RegisterClientScriptBlock(this.GetType(), "txtOnPaste", OnPasteFunction, true);
String OnDropFunction = "function verifyMaxLengthOnDragDrop(ref) {\n";
OnDropFunction += " var seltext;\n";
OnDropFunction += " var range;\n";
OnDropFunction += " var data;\n";
OnDropFunction += " var msg;\n";
OnDropFunction += "\n";
OnDropFunction += " if (ref.MaxLength != null) {\n";
OnDropFunction += " data = window.event.dataTransfer.getData('Text');\n";
OnDropFunction += " range = window.document.selection.createRange();\n";
OnDropFunction += " seltext = range.text;\n";
OnDropFunction += " if (seltext != null && seltext != '' && seltext != undefined) {\n";
OnDropFunction += " if ((ref.value.length - seltext.length) + data.length <= ref.MaxLength) {\n";
OnDropFunction += " return true;\n";
OnDropFunction += " }\n";
OnDropFunction += " }\n";
OnDropFunction += " if (ref.value.length + data.length > ref.MaxLength) {\n";
OnDropFunction += " if (ShowAlertIfMaxLength == true) {\n";
OnDropFunction += " msg = 'Le texte à déposer ne peut être contenu ';\n";
OnDropFunction += " msg += 'dans cette zone de saisie \\n'; \n";
OnDropFunction += " msg += 'limitée à ' + ref.MaxLength + ' caractères ';\n";
OnDropFunction += " alert(msg);\n";
OnDropFunction += " }\n";
OnDropFunction += " return false; // Si la ligne suivante fonctionnait il faudrait virer celle là\n";
OnDropFunction += " // window.event.dataTransfer.setData('Text',data.substring(0,ref.MaxLength-ref.value.length));\n";
OnDropFunction += " }\n";
OnDropFunction += " return true;\n";
OnDropFunction += " }\n";
OnDropFunction += "}\n";
ClientScript.RegisterClientScriptBlock(this.GetType(), "txtOnDrop", OnDropFunction, true);
}
// ==================================================================
// = Ici débute votre PageLoad
// ==================================================================
}
Conclusion
Les differents sources qui m'ont inspirés sont (c) by leurs auteurs respectifs... Merci à eux.
Historique
- 21 novembre 2007 01:33:17 :
- Correction de "fôtes" et suppression d'une phrase pas très sympa due à la fatigue... :)
désolé
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Textbox et textarea [ par leviz ]
Bonjour à vous! J'ai un petit problème : j'utilise dans ma page .aspx un textarea pour permettre à l'utilisateur de saisir une dé
Comment faire pour que plusieurs textbox successives se comportent comme un textarea [ par zoum2000 ]
Bonjour à tous,Alors voilà, j'ai un gros problème. Je ne sais pas si je suis sur le bon forum car je ne sais même pas si ce que je veux faire est poss
Textbox ASP.NET [ par gbrualla ]
Hello, probleme ASP.NET Je rencontre un problème avec l'utilisation d'une Textbox que j'ai déclaré avec les propriétés suivantes:TextMode=MultilineM
Textbox, maxlength (sous aspx) [ par Cookiem ]
Bonjours, en fait j'aurais voulu limité un textbox à 255 caractères.(comment ca c'est pour mettre dans une base access?Euh vi ...
taille textbox multiline [ par marliche0 ]
Je dois bloquer la taille de ma textbox à 2000 caractères et comme maxlength ne marche pas sur les textbox multiline j'ai créé une
textbox et base de données [ par marhoa ]
Voilà j'essaie de mettre dans un texbox les données récupérées gràce à une requète mais les textbox n'ont pas de propriété telles "DataSource" ou "Dat
configurer des textbox dynamiques [ par sev622 ]
bonjour,je remplit un tableau avec des textbox dynamiques auxquels je donne un id différent à chacun. Je voudrai pouvoir également d
FindControl - Acceder a mes controls crées dinamiquement [ par edokt ]
Bonjour Encore des problems J'ai une page aspx avec 2 bouton et un placeholder Premier bouton cree un control textbox dans le placeholder avec le
Lien dans une nouvelle fenêtre [ par zack67 ]
Bonjour à tous, je n'arrive pas à faire quelque chose de très simple.J'ai un textbox et un bouton, je voudrai que lorsque je clique sur
Rendre Actif un Textbox avec Visual Interdev [ par tjp88 ]
Bonjour,Je voudrai a l'ouverture de ma Page .ASP rendre 'actif' un textbox pour que l'utilisateur ne soit pas obliger de cliquer dedant avant d'inscri
|
Téléchargements
Logiciels à télécharger sur le même thème :
|