Disable Keyboard Entry to ASP Textbox

1

I have a text box with an ajaxToolkit:calendarextender attached to it.
I also have an OnTextChanged="do something" with an AutoPostBack=true.

I only want my user to be able to enter data into a textbox by using the calendar extender.

I've tried setting the readonly property on the textbox to true. This works only if there is existing text in the textbox (if I textbox.text = "such and such" then entry via the calendar extender works).

If I try selecting a date by clicking the textbox and selecting a date in the calendar extender, anything that is entered is immediately cleared because of the autopostback and the OnTextChanged event. I get this exception:

Exception was thrown at line 3169, column 13 in http://localhost:44994/ScriptResource.axd?d=Es_vqsdtlYssgAF4A3XSWnRVXoRhHI0Nw5kbnX4TQidZ-ER24J-iO0awsG5JH1rtQZR_iEqhZs7sdOBQ9bRTWyylEaGjH7fv-s8DBZ9g2dBrQRgBCGkgUkMcfnp1eLLpQ46PuRCK6kw9bzcz5Laedpe2sZMPd6VRGBt-E7Jq3Hi6WSFYwQmlvlft0Bho70p20&t=7b689585
0x800a138f - JavaScript runtime error: Unable to get property 'AbbreviatedMonthNames' of undefined or null reference

How can I disable entry into the textbox via keyboard and only allow entry by the calendarextender?

c#
asp.net
textbox
asked on Stack Overflow Jun 18, 2015 by gh0st • edited Jun 22, 2015 by gh0st

2 Answers

5

Add attribute onkeydown="return false;" to the textbox. This way user won't be able to type anything in, but the click will still work.

You may also want to add onpaste ="return false;" to prevent copy/pasting text.

answered on Stack Overflow Jun 22, 2015 by Yuriy Galanter
0

Another way, add it during page load at your code behind:

protected void Page_Load(object sender, EventArgs e)
{
    txtFrm.Attributes.Add("readonly", "true");
}

This will prevent typing and copy pasting input from user at once.

answered on Stack Overflow Jul 12, 2017 by LifeiSHot

User contributions licensed under CC BY-SA 3.0