简体   繁体   中英

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

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

让您的其他控件将值存储在隐藏字段中,并在回发时,从隐藏字段中提取值并将其推送到服务器端的文本框中。

txtStartDate.Attributes.Add("readonly", "readonly"); on pageload in the best of the best solutions ,instead or Javascripts,hidden variables,cache,cookies,sessions & Caches.

Get the value using Request.Form[txtDate.UniqueID] . You will get it !!

I've had this same issue but using Knockout binding 'enable' and ASP.Net Server Control Text.

This way:

<asp:TextBox ID="txtCity" runat="server" required="required" class="form-control" placeholder="City" data-bind="value: city, enable: !hasZipCode()"></asp:TextBox>

However, when the form was submitted this field value was always empty. This occurred, I presume, because if the control is disabled, it is not persist on the ViewState chain.

I solved replacing bindig 'enable' by 'attr{ readonly: hasZipCode}'

    <asp:TextBox ID="txtCity" runat="server" required="required" class="form-control" placeholder="City" data-bind="attr{ value: city, readonly: hasZipCode }">/asp:TextBox>

Here is a way to do it with javascript in the onfocus event of the Textbox itself.

Doing it like this with javascript has the advantage that you don't need to do it in code behind, which can be difficult if you need to do it in gridviews or similar.

This javascript code is only tested on Internet Explorer and certain parts of it will only work on IE, like for example the createTextRange part which is there just to make the caret end up at the beginning of the text in the Textbox, but that part can be skipped if not needed.

If the core of this technique works on other browsers then it should be possible to make the code cross browser. The core of the idea here is the blur after setting readonly and then a timeout to set the focus again.

If you only set readonly then it does not become readonly until next time you give the Textbox focus.

And of course, the code can be put into a function instead which is called with "this" as argument.

  <asp:TextBox 
    ID="txtSomething" 
    runat="server"
    Text='<%# Bind("someData") %>'
    onfocus="
var rng = this.createTextRange();
rng.collapse();
rng.select();
if (this.allowFocusevent=='0') {return;};
this.allowFocusevent='0';
this.readOnly=true;
this.blur();
var that=this;
setTimeout(function(){that.focus()},0);
"
  />

将文本框的 ContentEditable 属性设置为 false ContentEditable="false" .. 它不会允许您编辑文本框的内容,即;将使文本框只读,并且还会使回发后的值保留在文本框中.. 我认为它最简单的方法来做到这一点..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM