ReadOnly TextBox when Converting from .Net 1.1

I recently had to upgrade an ASP .net 1.1 solution to .net 3.5.  The website had a form where a user had an option to upload a file.

File Upload

The ‘File Name’ is a read only textbox.

  1. <asp:TextBox runat="server" ID="txtFileName" ReadOnly="true" />

When the ‘Upload’ button is clicked, a pop-up displays for the user to upload a file.  The ‘File Name’ texbox on the main form is populated with the selected file name path via javascript.  The problem occurs when the main form is saved, the new textbox value was not accessible in the code behind.  Since the ‘File Name’ textbox is read only, any client side changes to the textbox value is not persisted in the ViewState.  This was put in as a security measure  starting in .net 2.0.

To get around this issue, remove the ReadOnly attribute from the textbox and add the readonly attribute in code.

  1. txtFileName.Attributes.Add("readonly", "readonly")