Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of the Visible property in the case of a 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?

like image 743
Learner Avatar asked Feb 25 '23 13:02

Learner


2 Answers

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

like image 125
DanO Avatar answered Apr 30 '23 07:04

DanO


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.

like image 40
Cody Gray Avatar answered Apr 30 '23 08:04

Cody Gray