Click here to Skip to main content
Click here to Skip to main content

ViewState and Readonly Property of Textbox

, 24 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article helps in understanding the readonly property with view state.

Introduction

It has been under discussion for some time that ASP.NET textbox which has the Readonly property assigned true does not retain the server side or client side changes. The changes are getting ignored across postbacks.

Using the Code

Let’s understand both the scenarios:

When EnableViewState is Set to 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 textbox and two buttons. As by default EnableViewState property is true, we have not set it for the textbox.

<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" /> 
<br /> 
<asp:Button ID="btnServer" runat="Server" 
	Text="Server Side" OnClick="btnServer_Click" /> 
<asp:Button ID="btnClient" runat="Server" Text="Client Side" 
	OnClientClick="return changevalue();" />

Put this JavaScript function in the head section which modifies the text box value on the client side which is called on btnClient button.

<script language="javascript" type="text/javascript"> 
function changevalue()
{
    document.getElementById('TextBox1').value = 'Modified Sample Text';
    return false;
}
</script>

Now, in the page load section, set the text box value.

protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
         TextBox1.Text = "Read only text box";
}

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

protected void btnServer_Click(object sender, EventArgs e)
{
     Response.Write(TextBox1.Text);
}

Now, just execute the program.

Initially text box is having "Read only text box" . Now click on the Client Side button and you will see that the textbox value is changed to "Modified sample Text". Now click on server side button. You will see the Server side value assigned to the textbox 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 textbox with EnableViewState= "false" and two buttons:

<asp:TextBox ID="TextBox2" runat="server" EnableViewState= "false" ReadOnly="true" /> 
<br /> 
<asp:Button ID="btnServer1" runat="Server" Text="Server Side" 
	OnClick="btnServer1_Click" /> 
<asp:Button ID="btnClient1" runat="Server" Text="Client Side" 
	OnClientClick="return changevalue1();" />

Put this JavaScript function in the head section which modifies the textbox value on the client side:

<script language="javascript" type="text/javascript"> 
function changevalue1()
{
    document.getElementById('TextBox2').value = 'Modified Sample Text';
    return false;
}
</script>

In the page load section, set the textbox value.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        TextBox2.Text = "Read only text box";
}

On btnserver_click, just display the value of textbox using Response.write().

protected void btnServer1_Click(object sender, EventArgs e)
{
     Response.Write(TextBox2.Text);
}

Now, just execute the program.

Initially textbox has "Read only text box" . Now click on the Client Side button and you will see that textbox 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.

One interesting thing is if you assign any value to the readonly textbox with EnableViewState= "false" at design time, it maintains that value. For example,

<asp:TextBox ID="TextBox3" runat="server" 
	EnableViewState= "false" ReadOnly="true" Text="Sample Text" /> 

Now Textbox will maintain this value assigned at design time even after postback.

Let's go back to our second scenario:

Now, the question here is whether there is any way to retrieve the value from the server once it is lost. Yes, there is...

protected void btnServer1_Click(object sender, EventArgs e)
{
     Response.Write(Request.Form[TextBox2.UniqueID]);
}

The line, Request.Form[TextBox2.UniqueID], will give the last modified (whether on server or on client) value of textbox.

As we saw, there are a 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:

TextBox1.Attribute.Add("readonly","readonly");

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

Check out these other references:

Enjoy!

History

  • 24th February, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jQuery By Example
Technical Lead
India India
I am an experienced Software Developer with 6+ years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server).
 
Visit my blog and you can also follow me on Facebook
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionViewState and Readonly Property of Textbox (View) PinmemberDhanesh P M5-Nov-14 1:12 
GeneralMy vote 5 thanks Pinmembermiropetkov5-Feb-14 2:24 
QuestionA big THANKS !! PinmemberMember 169469814-Sep-12 18:26 
GeneralMy vote of 5 PinmemberSimon Dufour9-Aug-11 3:03 
GeneralSaved my day. PinmemberSrinivas-Miriyala21-May-09 6:07 
GeneralRe: Saved my day. PinmemberVirendra Dugar21-May-09 19:10 
GeneralSmall error Pinmemberjohram26-Apr-09 11:52 
GeneralRe: Small error PinmemberVirendra Dugar13-May-09 2:42 
GeneralMy vote of 1 PinmemberAcoustic24-Feb-09 3:48 
GeneralRe: My vote of 1 PinmemberVirendra Dugar24-Feb-09 18:13 
GeneralRe: My vote of 1 Pinmembergstolarov25-Feb-09 7:42 
GeneralRe: My vote of 1 PinmemberSleepwalker8717-Apr-09 0:16 
GeneralRe: My vote of 1 PinmemberCoding C#7-Jun-09 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 24 Feb 2009
Article Copyright 2009 by jQuery By Example
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid