public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

ViewState and Readonly Property of text box.

Posted by Virendra Dugar on March 1, 2009

Well, this has been under discussion for some time that ASP.Net TextBox which has the Readonly property assigned true not retaining the server side or client side changes. The changes are getting ignored across postbacks.  

Let’s understand both the scenario

When EnableViewState is true

When EnableViewState is set to true, then readonly textbox does maintain the server side changes but it does not maintain client side changes.

Let us understand with an example 

Declare a readonly text box and two buttons

 

Put this javascript function in the head section which modifies the text box value on the client side.

In the page load section, set the text box value.

On button click, just display the value of text box using Response.write().

Now, Just execute this program.

Initially text box is having “Read only text box” . Now click on the Client Side button and you will see that text box value is changed to “Modified sample Text”. Now click on server side button. You will see the Server side value assigned to the text box is displayed on the screen not the client side value.

When EnableViewState is False

When EnableViewState is set to false, then readonly textbox does not maintain the server side changes and client side changes.

Let us understand with an example

Declare a readonly text box with EnableViewState= false and two buttons.

Put this javascript function in the head section which modifies the text box value on the client side.

In the page load section,set the text box value.

On button click, just display the value of text box using Response.write().

Now, Just execute this program.

 Initially text box is having “Read only text box” . Now click on the Client Side button and you will see that text box value is changed to “Modified sample Text”. Now click on server side button. You will see that nothing is displayed on the screen. Textbox loses not only the client side value but also the server side value assigned to it. 

The interesting thing is if you assign any value to the readonly text box with enableviewstate = false at design time, it maintains that value.

 For example,

Textbox3 maintains it’s value assigned at design time.

Now, the question over here is that is there any way to retrieve the value from the server once it’s lost. Yes there is..

Request.Form[TextBox2.UniqueID], This line will give the last modified (Whether on server or on client) value of text box.

As we saw,there are number of concerns while using the readonly property. The best solution to avoid all these issues is in the code behind file, within the Page_Load add the following line of code:-

Now you will notice that when you run the page, the client side changes you make in the TextBox (via the Javascript) is retained across postback.

Check out these other reference :

http://west-wind.com/weblog/posts/3939.aspx

http://www.g9th.com/blog/post/2006/12/Readonly-property-of-Textbox-in-ASPNET.aspx

Enjoy…..

Advertisements

8 Responses to “ViewState and Readonly Property of text box.”

  1. ABC said

    Thanks !!!! it worked for me…

  2. anna said

    excellent post – saved me loads of time.

  3. Tony said

    Thank You!

    I had a weird issue where I have a popup form used for editing which was pre-populated using server-side code, and hidden. When submitting changes later, the code only saw the original server side generated values.

    This solved the issue completely.

  4. swetha said

    thank u so much……! it worked for me..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: