开发者

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

开发者 https://www.devze.com 2023-04-07 18:37 出处:网络
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 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");
}


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.


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


Set the ContentEditable property of textbox to false ContentEditable="false".. It wont allow you to edit the contents of the textbox ie;will make the textbox readonly and also will make the value stay in the textbox after postback.. I think its the easiest way to do it..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号