对于 HiddenField,Visible 属性有什么用?
据我了解,Visible
属性有助于启用或禁用控件的可见性。
但对于 ASP.NET 中的 HiddenField
控件来说,它有什么用呢?
The Visible
property, as I understand it, helps to enable or disable the visibility of a control.
But what is its use in the case of the HiddenField
control in ASP.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HiddenField 上的
Visible
属性的功能与其他控件上的功能类似。如果 HiddenField 控件的Visible
属性设置为false
,则该控件不会呈现到页面。通常 HiddenField 呈现为元素。但如果它不可见,则其数据将保存在页面的视图状态中。
在 .Net 2.0 中引入 HiddenField 的原因是作为
视为存储此类隐藏状态信息(当这些位置不可用或不需要时)的替代方案。将 Visible 设置为 false 只是强制它再次使用视图状态,而不是渲染
。因此,它有点违背了目的,但它是一个易于理解的容器,用于容纳用户不需要查看的一些数据。
无论它是呈现为文档中的元素(Visible = true)还是视图状态编码(Visible = false),都没有太大区别。
重要的是要知道,即使可见属性为 false,HiddenField 的值实际上也会随页面一起发送,并且不应用于敏感信息。
ASP.NET HiddenField 可见属性
wiki.ASP.NET 上的 ASP.NET HiddenField
The
Visible
property on a HiddenField functions like it does on other controls. If a HiddenField control has itsVisible
property set tofalse
, the control is not rendered to the page. Normally HiddenField is rendered as an<input type= "hidden"/>
element. But if it is not Visible, its data is held in the page's viewstate instead.The reason the HiddenField was introduced in .Net 2.0 was as an alternative to
as places to store that sort of hidden state information when those locations are either unavailable or undesirable. Setting Visible to false just forces it to use viewstate again instead of rendering the
<input type= "hidden"/>
. So it defeats the purpose a little, but it is a well-understood container for a bit of data the user doesn't need to see.Whether or not it is rendered as an element in the document (Visible = true) or viewstate encoded (Visible = false), doesn't make that much of a difference.
It is important to know that a HiddenField's value is in-fact sent with the page even when the visible property is false, and should not be used for sensitive information.
ASP.NET HiddenField Visible Property
ASP.NET HiddenField on wiki.ASP.NET
Visible
属性甚至出现在HiddenField
对象上,因为继承。文档表明
HiddenField
类 继承自基Control
类,定义Visible
属性。继承意味着从基类继承的所有类都会自动获得或获取基类公开的所有方法。在这种情况下,HiddenField
正在获取其基Control
类的Visible
属性,尽管您在问题中指出它显然毫无用处。这并不是因为语言经常有“愚蠢的事情”(尽管我不会质疑这种说法的准确性),而是因为面向对象的设计使得不可能从从基类继承的派生类中删除方法。
The
Visible
property is present even on aHiddenField
object because of inheritance.The documentation indicates that the
HiddenField
class inherits from the baseControl
class, which defines theVisible
property. Inheritance means that all classes who inherit from a base class automatically gain, or pick up, all of the methods exposed by the base class. In this case,HiddenField
is picking up theVisible
property of its baseControl
class, even despite it's apparent uselessness that you point out in the question.It's not because languages often have "silly things" (although I won't dispute the veracity of that claim), it's because an object-oriented design makes it impossible to remove methods from derived classes that are inherited from base classes.
可能是因为让它不存在比让它留在那里更难。
许多语言都有愚蠢的东西。就像 C 和 C++ 中的一元 + 运算符
Probably because it was harder to make it not be there than to leave it there.
Many languages have silly things. Like the Unary + operator in C and C++