Aug 8, 2012

ASP.NET AJAX toolkit calendar control with readonly textbox

I used the calendar control from the AJAX toolkit to set a date in its associated asp:textbox control. I wanted the user to only be able to set the date through the calendar control so I made the textbox to read only by setting the ReadOnly property to true. The problem this produces is that readonly controls don’t post back their values in the viewstate thus you lose the value when the user submits the form.

The Solution:

Leave the textbox control property as ReadOnly = False

In the Page Load event set the client side attribute of the textbox with the following:

txtDate.Attributes.Add("ReadOnly", "true");

This way the control is included in the postback and the value is preserved but client side the user won’t be able to manually edit it.

No comments:

Post a Comment