Wednesday, August 4, 2010

JavaScript : Disable Enter key events

In CS:

txtbox.Attributes.Add("onkeydown", "return DisableEnterKey(event);");

----------------
function DisableEnterKey(e) {
var key;
if (window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return (key != 13);
}

Javascript : Moving items from one list box to another.

function MoveItemsToListBox(lstbxFrom, lstbxTo, strMessage) {
var varFromBox = document.all(lstbxFrom);
var varToBox = document.all(lstbxTo);
if ((varFromBox != null) && (varToBox != null)) {
if (varFromBox.length < 1) {
alert('There are no items in the source ListBox');
return false;
}
if (varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1
{
alert(strMessage);
return false;
}
while (varFromBox.options.selectedIndex >= 0) {
var newOption = new Option(); // Create a new instance of ListItem
newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox
varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox
}
}
return false;
}