Pages

Wednesday, November 9, 2011

Making text box Readonly without using Readonly propery


Here I am explaing how to make a textbox readonly without using ReadOnly property of TextBox. The problem associated with ReadOnly is when we change the value of TextBox using readonly, it changes only in the UI. When TextBox value is accessed in codebehind it takes the default value instead of changed value.
To overcome above issue we can use two of javascript property onkeydown and oncontextmenu
<asp:TextBox ID="TextBox2" onkeydown="return false;" oncontextmenu="return false;" onfocus="return SetButtonFocus();" runat="server"Text="Default Value"></asp:TextBox>
This scenario is quite occuring in case of  javascript Date picker. Date changed in client side does not reflect in code behind. The other way of achieving this is by Disabling TextBox but it greys out text color which does not match with entire page layout.

Live Demo

No comments:

Post a Comment