Solution to capture read only textbox value in ASP.Net 2.0
2008-09-10
Today I am upgrading one of my applications from .Net framework 1.1 to 2.0. I found that the application is able to capture the values from all web controls except the date picker after the version upgrade.
The date picker is a read only textbox coming with an calendar icon. The client side Javascript will fill the value automatically when the user picks the date from the popup calendar.
Below is the code for the date picker text box
<asp:TextBox ID="txtRegistrationDate" ReadOnly="True" runat="server"/>
|
The output of txtRegistrationDate.Text is always empty when I pick a date and perform a postback. But I did capture the date picker value if I use the traditional method Request.Form["txtRegistrationDate"].
Then I found the notes as the followings from MSDN:
The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a post back occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only. The value of the Text property is preserved in the view state between postbacks unless modified by server-side code.
Finally I found a workaround to solve this issue:
1. Remove the ReadOnly="True" from the date picker text box
<asp:TextBox ID="txtRegistrationDate" runat="server"/>
|
2. In the page_load event, set the readonly client-side attribute.
txtRegistrationDate.Attributes.Add("readonly", "readonly");
|
Now the value for read only text box has been captured successfully.
Next Article:
Bring back the ChangePasswordTemplate from SuccessTemplate