Anas Ghanem

ASP.NET from the middle east

Syndication

Sponsors

News


    Subscribe in a reader
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 01, 2008 10:42 AM

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 07, 2008 7:46 AM

Or you can set the textbox to be readonly.

by N G

# re: How to get the readonly textbox value on the server @ Wednesday, February 18, 2009 3:20 AM

Thnx Anas,

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

rgds,

RS

by RS

# re: How to get the readonly textbox value on the server @ Tuesday, March 03, 2009 6:51 AM

thanx man

you help alooooooot

thanx again

by ahmed

# re: How to get the readonly textbox value on the server @ Tuesday, March 24, 2009 5:43 AM

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

Thank you

by MP

# re: How to get the readonly textbox value on the server @ Monday, March 30, 2009 11:21 AM

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

by Venkat

# re: How to get the readonly textbox value on the server @ Tuesday, March 31, 2009 1:36 AM

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

by dev

# re: How to get the readonly textbox value on the server @ Tuesday, March 31, 2009 6:50 AM

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

by aakash

# re: How to get the readonly textbox value on the server @ Tuesday, May 12, 2009 1:36 AM

great post man.

by amalraj

# re: How to get the readonly textbox value on the server @ Thursday, July 09, 2009 3:17 AM

Thnx Anas,

It helped me in same problem

By Amolraj

by Amol

# re: How to get the readonly textbox value on the server @ Thursday, October 15, 2009 8:31 AM

Very Nice tips dude

by RamSunder

Leave a Comment

(required) 
(required) 
(optional)
(required)