对于 HiddenField,Visible 属性有什么用?

发布于 2024-10-08 05:49:33 字数 111 浏览 3 评论 0原文

据我了解,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神仙妹妹 2024-10-15 05:49:33

HiddenField 上的 Visible 属性的功能与其他控件上的功能类似。如果 HiddenField 控件的 Visible 属性设置为 false,则该控件不会呈现到页面。通常 HiddenField 呈现为 元素。但如果它不可见,则其数据将保存在页面的视图状态中。

在 .Net 2.0 中引入 HiddenField 的原因是作为

  • 将状态
  • 会话状态
  • cookie

视为存储此类隐藏状态信息(当这些位置不可用或不需要时)的替代方案。将 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 its Visible property set to false, 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

  • view state
  • session state
  • cookies

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

孤独陪着我 2024-10-15 05:49:33

Visible 属性甚至出现在 HiddenField 对象上,因为继承

文档表明HiddenField 继承自基 Control,定义 Visible 属性。继承意味着从基类继承的所有类都会自动获得或获取基类公开的所有方法。在这种情况下,HiddenField 正在获取其基 Control 类的 Visible 属性,尽管您在问题中指出它显然毫无用处。

这并不是因为语言经常有“愚蠢的事情”(尽管我不会质疑这种说法的准确性),而是因为面向对象的设计使得不可能从从基类继承的派生类中删除方法。

The Visible property is present even on a HiddenField object because of inheritance.

The documentation indicates that the HiddenField class inherits from the base Control class, which defines the Visible 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 the Visible property of its base Control 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.

屋顶上的小猫咪 2024-10-15 05:49:33

可能是因为让它不存在比让它留在那里更难。

许多语言都有愚蠢的东西。就像 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++

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文