Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET hidden field vs. invisible textbox

Tags:

asp.net

what are benefits of using a hidden field in ASP.NET when we can use another invisible element such as label or text box?

like image 754
Ali_dotNet Avatar asked Dec 09 '11 14:12

Ali_dotNet


People also ask

What is a hidden field in asp net?

HiddenField, as name implies, is hidden. This is non visual control in ASP.NET where you can save the value. This is one of the types of client-side state management tools. It stores the value between the roundtrip. Anyone can see HiddenField details by simply viewing the source of document.

What is the difference between view state and hidden field?

In View state - not able to change the value by Client side code i.e java script. Hidden field - possible to change value by Client side code. Hidden field - You can store more than one value in hidden field,by serialized it.

How do I hide an asp text box?

Answers. The Visible=false property will preserve the space for a specifc control while style="display:none" will hide the textbox and doesnt preserve the space.

What is the concept hidden field?

A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.


4 Answers

The hidden field generate <input type="hidden" /> element on the page, which cannot be seen but the client can get the element, set the data and pass to the server:

document.getElementById('<%= SomeHiddenField.ClientID %>').value = "data_pass_to_server";

after postback you can get the value:

var clientData = SomeHiddenField.Value; // "data_pass_to_server"

If you're using invisible textbox (<asp:TextBox Visible="False" />), there's no element generated in the html file.

like image 71
Jeffrey Zhao Avatar answered Oct 27 '22 20:10

Jeffrey Zhao


Either way works, for text box, don't use .visible="false" use

yourTextBox.Style.Add("display", "none")

or

yourTextBox.Style.Add("visibility", "hidden")
like image 23
QianXue Avatar answered Oct 27 '22 18:10

QianXue


A hidden field renders as input type="hidden" in the resulting HTML. Being an input the value in the input is submitted to the server on postback while this is not the case with a label. Depending on whether or not you want that value submitted to the server you should use input or label. If you don't want the value to be submitted then label is the right solution and hidden field is wrong.

I am not sure what you mean by invisible textbox but if you are trying to make it invisible via CSS keep in mind that the input type has semantic meaning to search engines, bots, etc. Also at some point your HTML might be served without CSS or with different CSS and the text box will become visible to the user. Otherwise there are no differences between hidden field and invisible text box as both of them render inputs.

like image 38
Stilgar Avatar answered Oct 27 '22 20:10

Stilgar


Practically you can achieve the same thing with any of them, but since you want a "hidden field", semantically speaking the hidden field in ASP.NET is your best bet for readability reasons.

like image 39
Pasman Avatar answered Oct 27 '22 19:10

Pasman