ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback
 
Rating: 66 user(s) have rated this article 4.3 out of 5 stars
Posted by: josh_admin, on 3/15/2008, in category "Working with HTML"
Views: this article has been read 1911 times


Delicious Bookmark this on Delicious

While creating some basic forms to capture user data I started using a jQuery calendar on a readonly textbox to ensure I was receiving correctly formatted data from the client. However, during the post back the code-behind was getting the value that was there from the page's last post-back/page_load, ignoring whatever value the jQuery calendar had set in the read-only textbox. The work around that I found was to not set the input box's readonly only attribute inline but rather in the code-behind in the Page_Load event:

protected void Page_Load(object sender, EventArgs e){ 

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

}

 

 

 

What caused my error

      I created a page that is used to add/edit data via a form.  When the page is invoked in the 'edit' mode than a function is called during the page load event and populates all the form fields with their corresponding values.  When I had used the TextBox control's ReadOnly attribute the value would be set and a jQuery calendar was associated to that control to allow updating the value. 

By setting the readonly attribute I was helping with client side validation.  But whenever the page would post back and call the update function, the control would give me the value originally set in the page_load event.  I really didn't want to change the TextBox to a non-readonly status so I explored other options.  The solution that I found involved setting the ReadOnly attribute in the code-behind file during the Page_Load event (as described above). 

 

Share and Bookmark

LinkedIn Google Del.icio.us Digg Reddit Stumble Upon

DISCLAIMER: This information is provided "as is". The author, publishers and marketers of this information disclaim any loss or liability, either directly or indirectly as a consequence of applying the information presented herein, or in regard to the use and application of said information. No guarantee is given, either expressed or implied, in regard to the merchantability, accuracy, or acceptability of the information.


How would you rate this article?

User Feedback
Comment posted by Di on Thursday, September 18, 2008 9:34 AM
Thank you very much - saved me half an hour research!
Comment posted by Silverfox on Saturday, January 10, 2009 7:46 AM
Had wasted a day before this post. Thanks Sir for the post!
Comment posted by Jim on Thursday, August 13, 2009 7:58 AM
Simple solution, but saved me a ton of research and head scratching.

Post your comment
Name:
Comment:
Insert Cancel
Questions...
Have you upgraded to Windows 7?


Archived Polls