We have found that text area (Multiline textbox) control in asp.net doesn't support the multiline property even you define that property.
so here is the solution for this problem
Code in .net Page
protected override void OnPreRender(EventArgs e)
{
if (MaxLength > 0 && TextMode == TextBoxMode.MultiLine)
{
// Add javascript handlers for paste and keypress
Attributes.Add("onkeypress", "doKeypress(this);");
Attributes.Add("onbeforepaste", "doBeforePaste(this);");
Attributes.Add("onpaste", "doPaste(this);");
// Add attribute for access of maxlength property on client-side
Attributes.Add("maxLength", this.MaxLength.ToString());
// Register client side include - only once per page
}
}
Code in JavaScript file
// Keep user from entering more than maxLength characters
function doKeypress(control)
{
maxLength = control.attributes["maxLength"].value;
value = control.value;
if(maxLength && value.length > maxLength-1)
{
event.returnValue = false;
maxLength = parseInt(maxLength);
}
}
// Cancel default behavior
function doBeforePaste(control)
{
maxLength = control.attributes["maxLength"].value;
if(maxLength)
{
event.returnValue = false;
}
}
// Cancel default behavior and create a new paste routine
function doPaste(control)
{
maxLength = control.attributes["maxLength"].value;
value = control.value;
if(maxLength)
{
event.returnValue = false;
maxLength = parseInt(maxLength);
var oTR = control.document.selection.createRange();
var iInsertLength = maxLength - value.length + oTR.text.length;
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;
}
}
using this code you can even check the copy paste and restrict the number of characters
now create a custom control and then use that control
set MaxLenght property of that control and you will be able to restrict the number of characters
Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts
Monday, August 13, 2007
Thursday, July 26, 2007
Show JavaScript Message While Closing IE Without Saving
Hello Friends
Following is the script to show message while navigating away from the current screen in browser.
var blnConfirmNavigation=true;
window.onbeforeunload = function (evt)
{
blnDeleteForum=false;
if(blnConfirmNavigation)
{
var message = 'You have not save the content of the page. Your changes will not be saved.';
if (typeof evt == 'undefined')
{
evt = window.event;
}
if (evt)
{
evt.returnValue = message;
}
blnDeleteForum=true;
return message;
}
}
Enjoy!
Subscribe to:
Posts (Atom)