How to get the readonly textbox value on the server

In ASP.NET 2.0 and Newer versions , the readonly TextBox will ignore the submitted text , this change due to security reasons . 

However , assume you have a readonly textbox and you are going to change its value on the client side ( via script) , Now how you will get the changed value on the server side ? One option is to disable the textbox control ,and set "SubmitDisabledControls" for the form to true ,  but this is not always a solution .

For example you want to use the Calendar extendar with the textbox and you don't want to let the user to change the textbox manually , instead you want to let the user select the date via the calendar extendar. now the problem is that the selected date will not be accepted by the textbox when the user submit the form , because the textbox is in readonly mode , in this case you need to manually set the submitted textbox value , you can easily use the values that is submitted via form collection ,

something like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load TextBox1.Text = Server.HtmlEncode(Request.Form(TextBox1.UniqueID)) End Sub

There is also many solutions other than setting the textbox as readonly , for example you can cancel the onCopy,onPaste,onkeyup all the required javascript events .


Anas Ghanem


Comments have been disabled for this content.