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()
.
One of the common interview question is “Explain the use of ViewState for TextBox?” and the most popular and common answer for this question is view state will maintain the text property value of the TextBox after a PostBack of the page.
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"); }
Have your other control store the value in a hidden field, and on postback, pull the value from the hidden field and push it into the textbox on the server side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With