ASP.NET Custom Control - Cannot Revert Modified Client-Side (JavaScript) Value Back
Ok guys and girls, here is my problem:
I have created a custom control that uses a textbox to present data to the user.
When the user interacts with the control, the value of this textbox is changed using client-side javascript .
I also have a button on my page. When the user clicks on the button, I want to take the value from the custom control (aka. Textbox) and use it elsewhere.
So, in the onClick event for the button, I do something like this:
this.myLabel.Text = this.customControl.Value;
The problem is that the custom control doesn't have a new textbox value available. In the custom control, the text box is empty. However, I can see the correct value in the Request.Form collection.
Am I doing something wrong here? Or should I be reading from Request.Form ?!
source to share
Aha! I solved my problem!
Because I set Readonly = "True" on the textbox control, ASP.NET didn't pick that value from the postback.
Instead, I had to manually add the readonly attribute to the textbox during my custom control design.
eg.
this.textBox.Attributes.Add("readonly", "readonly");
source to share
Interestingly, I didn't realize that the read-only TextBox is not being updated from the viewstate.
When I do tricks like these on my websites, I usually set up asp: HiddenFields which I will give push into javascript (gotta love jQuery) and that I read values from postbacks.
Keeps things cleaner, I find.
source to share
It is strange that you answered yourself! In fact, I ran into this nuisance before and cost me time until I found a note in the visual studio documentation describing the reason, you can read it here http://msdn.microsoft.com/en-us/library/system.web .ui.webcontrols.textbox.readonly.aspx in the Important Note section.
source to share