How to keep the Text of a Read only TextBox after PostBack()?

Youssef picture Youssef · Sep 27, 2011 · Viewed 37.5k times · Source

I have an ASP.NET TextBox and I want it to be ReadOnly. (The user modify it using another control)

But when there is a PostBack(), The text get reset to an empty string.

I understand that if you set the ReadOnly property to True of a TextBox it's content does not get saved through PostBack().

Is there a way to keep the content after PostBack() and make the TextBox not editable by the user?

I tried to set the Enabled property to False,But still the content doesn't save after PostBack().

Answer

Youssef picture Youssef · Oct 1, 2011

Another solution I found and easier one:

Add this to the Page Load method:

protected void Page_Load(object sender, EventArgs e)
{
     TextBox1.Attributes.Add("readonly", "readonly");
}