Friday 20 August 2010

Read Text value from ReadOnly TextBox

Some time we need to make textbox read-only for showing data. If we make textbox read-only at the designer setting as below, then the textbox always returns blank in the code behind.



Here I am giving you an example of the issue and the solution.
For date picker I used ASP.NET AJAX 's Calendar extender that can be attached to any ASP.NET TextBox control only. And in the textbox I don't want that user directly write any text.
For achieving this, if I make textbox read-only then at the server side textbox.text will come as blank(empty).
Solution




In code behind add an attribute.

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

By doing this we can retrieve read-only textbox's value in the code behind.