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 .

Regards,

Anas Ghanem

Published Monday, July 28, 2008 8:13 PM by anas
Filed under:

Comments

# re: How to get the readonly textbox value on the server

Friday, August 1, 2008 10:42 AM by mr.aspdotnet

Nice tip Anas, I remember going into this problem then my solution was to copy the value to a hidden field before submitting which was a messy solution. Thanks

# re: How to get the readonly textbox value on the server

Tuesday, October 7, 2008 7:46 AM by N G

Or you can set the textbox to be readonly.

# re: How to get the readonly textbox value on the server

Wednesday, February 18, 2009 3:20 AM by RS

Thnx Anas,

I used to set 'event.returnValue=false' in the 'onkeypress' event (javascript) to make the textbox   ""readonly"" ..!!!!.

rgds,

RS

# re: How to get the readonly textbox value on the server

Tuesday, March 3, 2009 6:51 AM by ahmed

thanx man

you help alooooooot

thanx again

# re: How to get the readonly textbox value on the server

Tuesday, March 24, 2009 5:43 AM by MP

Tahnx man....the exact solution to my problem!!!

Thank you

# re: How to get the readonly textbox value on the server

Monday, March 30, 2009 11:21 AM by Venkat

Thanks for you ......valuable tips...........thanks lot..................

# re: How to get the readonly textbox value on the server

Tuesday, March 31, 2009 1:36 AM by dev

very Nice tips, when use this code i get solution so thanks very much

# re: How to get the readonly textbox value on the server

Tuesday, March 31, 2009 6:50 AM by aakash

This piece of information help me avaiding hours of efforts. Great work dude!

# re: How to get the readonly textbox value on the server

Thursday, July 9, 2009 3:17 AM by Amol

Thnx Anas,

It helped me in same problem

By Amolraj

# re: How to get the readonly textbox value on the server

Thursday, October 15, 2009 8:31 AM by RamSunder

Very Nice tips dude

# re: How to get the readonly textbox value on the server

Monday, January 4, 2010 5:05 AM by Abdul wakeel

When you set the textbox readonly property true

then the server do  not store its view state . so that is why you can not get data from text box as a text ./

# re: How to get the readonly textbox value on the server

Thursday, June 17, 2010 1:23 AM by Talha Karkun

thanks Anas

u have save my loat of time for this problem

# re: How to get the readonly textbox value on the server

Thursday, August 26, 2010 11:09 AM by Raghav

Thanks Anas.  It worked for me.

# re: How to get the readonly textbox value on the server

Monday, November 29, 2010 8:04 AM by Juan

Its so simple and works like a charm..

Thanks Anas

# re: How to get the readonly textbox value on the server

Monday, January 3, 2011 9:51 AM by Antonias

Thanks this realy help me,

this is c# version

TextBox1.Text = Server.HtmlEncode(Request.Form[TextBox1.UniqueID]);

# re: How to get the readonly textbox value on the server

Saturday, April 23, 2011 4:31 PM by sunshine57

hi, i have attached a string to text box. i do not want user to modify this string. In addition, if the user wants to add something in this text box, only added text should be saved to database when clicked not the whoe textbox value.

please advice

Anjali singh

# re: How to get the readonly textbox value on the server

Wednesday, June 29, 2011 1:57 AM by NewBie to Asp.net

Thanks a Ton . This has solved my problem.  God bless you.

# re: How to get the readonly textbox value on the server

Thursday, September 15, 2011 7:59 AM by BMD

Thanks a lot man...

# re: How to get the readonly textbox value on the server

Wednesday, May 30, 2012 12:56 PM by Marcos

This piece of information help me avoiding hours of research, so thanks very much.