|
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
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).