Tuesday, August 21, 2007

Javascript to stop Editing of a Textbox

Here's a quick bit of Javascript to stop someone from entering data into a text box. Why would you need this when you can use a label or a read only text box?

1. The label is not a control you can get the .text from with code behind.
2. The read only text box will always return String.Empty when referenced with the code behind

Warning, that this code does not stop the delete key from being hit.

//Do not allow user to enter data into Fleet Type
function FleetKeyPress()
window.event.keyCode = 0;


{{ asp:textbox onkeypress="javascript:FleetKeyPress();" id="txtFleetType" cssclass="txtbox" columns="8" runat="server" }}

No comments: