Mindfire Solutions
Home  |  Faq  |  Site map  |  Contact
Email sales@ or Call 1-248-686-1424
Share on Facebook Share on Twitter Share On Linkedin  
Share
How to retrieve read-only textbox's value in the code behind
Author: Sumit Singh
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.
 
<asp:TextBox ID="txtDate" runat="server" ReadOnly="true">
 
 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

<asp:TextBox ID="txtDate" runat="server">

<act:CalendarExtender ID="txtdate_CalendarExtender" runat="server" TargetControlID="txtdate" PopupButtonID="txtDate" Format="MM/dd/yyyy"></act:CalendarExtender>

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.

Related Tags:
ASP.NET, ReadOnly

top