Search code examples
c#asp.nettextboxpostbackreadonly

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


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


Solution

  • 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");
    }