Microsoft Developer
Microsoft Developer

Reputation: 5459

Keep textbox protected from keyboard typing

I have a textbox for date. It has calender control extender to select date. I want to keep this textbox type protected. i.e. user must enter date by selecting date from calender control only and not allowed to type the date. I tried to keep textbox read-only but it is not solving the purpose.. How can I do so...???

Upvotes: 0

Views: 883

Answers (4)

Guilherme Nagatomo
Guilherme Nagatomo

Reputation: 306

If the readonly attribute isn't working, try setting the onkeydown event of the input to return false.

document.getElementById("input").onkeydown = function(){return false;}

Upvotes: 0

M S
M S

Reputation: 4093

try disabled attribute.

<input disabled="disabled"></input>

If you do that the background color will be gray. To make it white you can do

<input disabled="disabled" style="background-color:white"></input>

use "style" attribute or css to style it appropriately.

Upvotes: 1

Xavi L&#243;pez
Xavi L&#243;pez

Reputation: 27880

Try the readonly attribute.

Using disabled can be inconvenient if you want to include the input's value as a parameter at <form> submission (disabled components do not make it to the request when processing the <form>).

<input readonly="readonly"></input>

Upvotes: 0

IUnknown
IUnknown

Reputation: 22448

I'm suspect that there is a problem with ReadOnly property that textbox loses his state on postback. Try to use readonly attribute instead of the ReadOnly property. Fot that purpose add following code to Page_PreRender method: TextBox1.Attributes["readonly"] = "readonly";

Upvotes: 1

Related Questions