问题描述
所以,我完成了在Web应用程序,做一些数据的自动插入一些改进。有文本框的少数用户填写,并在它们之间有自动计算值的其他文本框。接收自动计算值,该文本框必须要readOnly的prevent用户的变化,但不能禁用或他们不会得到回发提交。每个文本框也有旁边有一个复选框,这样用户就可以有意识地检查一下以现场写,从而使他们能够覆盖插入值。
So I was finishing up some enhancements in a web application that does some automatic interpolation of data. There are a handful of textboxes that the user fills out, and other textboxes in between them have values automatically calculated. The textboxes that receive automatically calculated values must be readOnly to prevent user changes, but can't be disabled or they won't get submitted on postback. Each of these textboxes also has a checkbox next to it so a user can consciously check it to make the field writable and thus allow them to override the interpolated value.
浏览器:IE 7(不知道这是如何的行为在别人)
The browser: IE 7 (not sure how this behaves in others)
当设置使用JavaScript文本框的 readOnly的
属性,在文本框中的值被提交的表单:我可以看到它在服务器端(ASP.NET )与 myTextBox.Text
及其在的Request.Form(myTextBox)
。
When setting the readOnly
property of the textboxes with JavaScript, the value in the textbox is submitted with the form: I can see it on the server side (ASP.NET) with myTextBox.Text
and it's in Request.Form("myTextBox")
.
如果我设置只读=真
我的&LT上; ASP:文本框/>
元素和唐'T使用JavaScript方法,在文本框中的值是不会从 myTextBox.Text
(我假设它从未进入的的ViewState
),但它越来越与表单提交:的Request.Form(myTextBox)
具有值
If I set ReadOnly="true"
on my <asp:TextBox />
element and don't use the JavaScript method, the value in the text box is NOT available from myTextBox.Text
(I assume it never made it into the ViewState
), but it is getting submitted with the form: Request.Form("myTextBox")
has a value.
到底是什么回事?这是设计?这是一个浏览器的问题?难道我发现了错误?这很烦人,我必须有一些额外的JavaScript最初禁用文本框的可写时,才能在页面加载,使我的应用程序的工作。
What the heck is going on? Is this by design? Is this a browser issue? Did I find a bug? It's annoying that I have to have some extra JavaScript to initially disable the writability of the textboxes when the page loads in order to make my app work.
谢谢!
推荐答案
这是由设计。作为一个ASP.NET安全功能,将其设置为只读在服务器上会保持它的只读,无论在客户端上会发生什么。如果你希望他们能够超越它,实际上提交值,那么它不是真正只读在服务器上,只有有条件地在客户端上。你可以做一个回发时,他们选中复选框,更改服务器上的只读属性,或使用该ASP.NET code中的客户端上只有只读属性:
This is by design. As an ASP.NET security feature, setting it to readonly on the server will keep it readonly regardless of what happens on the client. If you want them to be able override it and actually submit a value then it's not really readonly on the server, only conditionally on the client. You can either do a postback when they check the checkbox to change the readonly attribute on the server, or set only the readonly attribute on the client using this ASP.NET code:
MyControl.Attributes.Add("readOnly","readOnly")
这篇关于奇怪的行为使用HTML'只读=&QUOT;只读&QUOT;'与JavaScript的'element.readOnly = TRUE;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!