Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 506
Y2theZ Avatar asked Sep 27 '11 14:09

Y2theZ


People also ask

How can we keep TextBox value after PostBack in asp net?

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.


2 Answers

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"); } 
like image 141
Y2theZ Avatar answered Sep 19 '22 13:09

Y2theZ


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.

like image 32
The Evil Greebo Avatar answered Sep 17 '22 13:09

The Evil Greebo