Click here to Skip to main content
15,868,101 members
Articles / Web Development / XHTML

ViewState and Readonly Property of Textbox

Rate me:
Please Sign up or sign in to vote.
4.64/5 (13 votes)
24 Feb 2009CPOL3 min read 107.1K   11   13
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.NET
<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.

JavaScript
<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.

C#
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().

C#
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.NET
<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:

JavaScript
<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.

C#
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().

C#
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.NET
<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...

C#
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:

C#
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)


Written By
Technical Lead
India India
I am an experienced Software Developer with 11+ years of hands-on experience working with Microsoft.NET technology (ASP.NET, ASP.NET Core, C#, SQL Server, Angular).

Visit Talking Dotnet
For ASP.NET Core, read ASP.NET Core Articles

Comments and Discussions

 
QuestionViewState and Readonly Property of Textbox (View) Pin
Dhanesh P M5-Nov-14 0:12
Dhanesh P M5-Nov-14 0:12 
GeneralMy vote 5 thanks Pin
miropetkov5-Feb-14 1:24
miropetkov5-Feb-14 1:24 
QuestionA big THANKS !! Pin
Member 169469814-Sep-12 17:26
Member 169469814-Sep-12 17:26 
GeneralMy vote of 5 Pin
Simon Dufour9-Aug-11 2:03
Simon Dufour9-Aug-11 2:03 
GeneralSaved my day. Pin
Srinivas-Miriyala21-May-09 5:07
Srinivas-Miriyala21-May-09 5:07 
GeneralRe: Saved my day. Pin
Talking Dotnet21-May-09 18:10
Talking Dotnet21-May-09 18:10 
GeneralSmall error Pin
johram26-Apr-09 10:52
johram26-Apr-09 10:52 
GeneralRe: Small error Pin
Talking Dotnet13-May-09 1:42
Talking Dotnet13-May-09 1:42 
GeneralMy vote of 1 Pin
TylerBrinks24-Feb-09 2:48
TylerBrinks24-Feb-09 2:48 
GeneralRe: My vote of 1 Pin
Talking Dotnet24-Feb-09 17:13
Talking Dotnet24-Feb-09 17:13 
GeneralRe: My vote of 1 Pin
gstolarov25-Feb-09 6:42
gstolarov25-Feb-09 6:42 
GeneralRe: My vote of 1 Pin
Sleepwalker8716-Apr-09 23:16
Sleepwalker8716-Apr-09 23:16 
GeneralRe: My vote of 1 Pin
Coding C#7-Jun-09 22:48
Coding C#7-Jun-09 22:48 

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

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