如何在 PostBack() 之后保留只读文本框的文本?
我有一个 ASP.NET TextBox
,我希望它是 ReadOnly
。 (用户使用另一个控件修改它)
但是当有 PostBack()
时,文本将重置为空字符串。
据我所知,如果您将 TextBox
的 ReadOnly
属性设置为 True
,则其内容不会通过 PostBack().
有没有办法保留 PostBack()
之后的内容并使用户无法编辑 TextBox
?
我尝试将 Enabled
属性设置为 False
,但在 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()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我找到了另一个更简单的解决方案:
将其添加到页面加载方法中:
Another solution I found and easier one:
Add this to the Page Load method:
让其他控件将值存储在隐藏字段中,并在回发时,从隐藏字段中提取值并将其推送到服务器端的文本框中。
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");
在最好的解决方案中的页面加载上,而不是Javascript、隐藏变量、缓存、cookies、会话等。缓存。txtStartDate.Attributes.Add("readonly", "readonly");
on pageload in the best of the best solutions ,instead or Javascripts,hidden variables,cache,cookies,sessions & Caches.使用
Request.Form[txtDate.UniqueID]
获取值。你会得到它!Get the value using
Request.Form[txtDate.UniqueID]
. You will get it !!我也遇到过同样的问题,但使用 Knockout 绑定“启用”和 ASP.Net 服务器控制文本。
这样:
但是,当提交表单时,该字段值始终为空。我认为发生这种情况是因为如果禁用该控件,它就不会保留在 ViewState 链上。
我解决了将 bindig 'enable' 替换为 'attr{ readonly: hasZipCode}'
I've had this same issue but using Knockout binding 'enable' and ASP.Net Server Control Text.
This way:
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}'
这是在文本框本身的 onfocus 事件中使用 javascript 来完成此操作的方法。
使用 javascript 这样做的优点是您不需要在后面的代码中执行此操作,如果您需要在 gridviews 或类似的中执行此操作,这可能会很困难。
此 JavaScript 代码仅在 Internet Explorer 上进行了测试,并且其中的某些部分仅适用于 IE,例如 createTextRange 部分,该部分只是为了使插入符号最终出现在文本框中文本的开头,但是该部分可以如果不需要可以跳过。
如果该技术的核心适用于其他浏览器,那么应该可以使代码跨浏览器。这里的核心思想是设置只读后模糊,然后超时再次设置焦点。
如果您仅设置只读,那么直到您下次给予文本框焦点时它才会变为只读。
当然,代码可以放入一个函数中,而不是用“this”作为参数来调用。
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.
将文本框的 ContentEditable 属性设置为 false ContentEditable="false"..
它不允许您编辑文本框的内容
即;将使文本框只读并且
回发后也会使值保留在文本框中。
我认为这是最简单的方法..
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..