ASP.NET AJAX toolkit calendar control with a readonly textbox

Posted in ASP.NET

The problem

I recently used the calendar control from the AJAX toolkit to set a date in its associated textbox control. I wanted the user to only be able to set the date through the calendar control so I made the textbox 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

After some research the easiest workaround I found was this.

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.

Posted by Darin Shaw   @   12 February 2010
Tags :

 

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

You must be logged in to post a comment.

Previous Post
«
Next Post
»
Powered by Wordpress   |   Lunated designed by ZenVerse