简体   繁体   English

如何在 PostBack() 后保留只读文本框的文本?

[英]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 .我有一个 ASP.NET TextBox ,我希望它是ReadOnly (The user modify it using another control) (用户使用另一个控件修改它)

But when there is a PostBack() , The text get reset to an empty string.但是当有PostBack() ,文本被重置为空字符串。

I understand that if you set the ReadOnly property to True of a TextBox it's content does not get saved through PostBack() .我知道,如果您将TextBoxReadOnly属性设置为True ,则其内容不会通过PostBack()保存。

Is there a way to keep the content after PostBack() and make the TextBox not editable by the user?有没有办法在PostBack()之后保留内容并使TextBox不能被用户编辑?

I tried to set the Enabled property to False ,But still the content doesn't save after PostBack() .我试图将Enabled属性设置为False ,但在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.在最佳解决方案中的最佳页面加载,而不是 Javascript、隐藏变量、缓存、cookie、会话和缓存。

Get the value using Request.Form[txtDate.UniqueID] .使用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.我有同样的问题,但使用 Knockout 绑定“启用”和 ASP.Net 服务器控制文本。

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.我认为这是因为如果控件被禁用,它就不会持久存在于 ViewState 链上。

I solved replacing bindig 'enable' by 'attr{ readonly: hasZipCode}'我解决了用 'attr{ readonly: hasZipCode}' 替换 bindig 'enable'

    <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.这是一种在 Textbox 本身的 onfocus 事件中使用 javascript 的方法。

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.用 javascript 这样做的好处是你不需要在后面的代码中做,如果你需要在 gridviews 或类似的东西中做这件事可能会很困难。

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.此 javascript 代码仅在 Internet Explorer 上进行测试,并且它的某些部分仅适用于 IE,例如 createTextRange 部分,它只是为了使插入符最终出现在文本框中的文本开头,但该部分可以如果不需要,可以跳过。

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.当然,代码可以放入一个函数中,而不是用“this”作为参数调用。

  <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" .. 它不会允许您编辑文本框的内容,即;将使文本框只读,并且还会使回发后的值保留在文本框中.. 我认为它最简单的方法来做到这一点..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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