Scenario:
Some times we might need to display the value in text box with read only property. But on some events, we would have to change it and finally retrieve the text box value from code behind.
Example:
a text box with calendar tagged to it. Here the text box is set with read only property, but the values is changed based on the date selected with the calendar control. and finally we try to retrieve the date selected with the help of text box value.
Problem:
When we try to get retrieve the read only text box value from code behind it just returns blank.
In the above mentioned example, when the user changes the date, then the selected date will be displayed in the text box. so we assume that the text box is getting updated properly. but then, when we try to get it value from code behind, it just returns blank.
Solution:
in code behind, give
textbox.Attributes.Add("readonly", "readonly");
By doing this we could set the text box with read only property, also could retrieve the text box value from code behind.
Hi,
ReplyDeleteThanks for the fix.
But it is to be noted that the particular textbox's 'ReadOnly' property must be set to 'false' in the design. Otherwise, textbox.Attributes.Add("readonly", "readonly") in the code behind will not be effective.
I made this mistake and rectified it, so I wish to share the same :)