开发者

What is the use of the Visible property in the case of a HiddenField?

开发者 https://www.devze.com 2023-01-31 01:09 出处:网络
The Visible开发者_如何学Python property, as I understand it, helps to enable or disable the visibility of a control.

The Visible开发者_如何学Python 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?


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


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.


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++

0

精彩评论

暂无评论...
验证码 换一张
取 消