Why does a read-only textbox not return any data in ASP.NET?

I’ve set a textbox to read-only. When the user clicks on it, a calendar is displayed and the user selects the date which inputs into the read-only textbox.

But when I try to enter the data into the database, it shows null value. What is wrong?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There is a little bit of strangeness when it comes to the ASP.NET Readonly property and the readonly attribute of an HTML input element. Rather than setting the Readonly property of the web control try simply adding the HTML attribute to the control like this:

textBox.Attributes.Add("readonly", "readonly");

This will make the control read-only in the client’s browser yet still allow you to retrieve the value of the input when it posts back to the server.

Method 2

The system assumes that read only or disabled controls won’t be changed clientside so it doesn’t post the changed value back to the server. You need to set the client side readonly property rather than the serverside version.

Method 3

ASP.NET assumes that the Readonly and Enabled=”false” property of the webcontrol will not change . Thus it does not postback the value as mentioned by tom above . You can use this workaround . Adding this to the html you don have to worry about Readonly property or disabled control .

   onkeydown="return false;"

Method 4

Or you can replace the ASP textbox with input type text with runat=”Server” and then adding readonly property as readonly. Check the link

Workarounds to access the Readonly Textbox value on server side when changed through client side script

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x