ASP.NET hidden field vs. invisible textbox

前端 未结 4 1077
野性不改
野性不改 2021-02-20 03:16

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

相关标签:
4条回答
  • 2021-02-20 03:53

    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.

    0 讨论(0)
  • 2021-02-20 04:02

    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.

    0 讨论(0)
  • 2021-02-20 04:04

    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.

    0 讨论(0)
  • 2021-02-20 04:07

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

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

    or

    yourTextBox.Style.Add("visibility", "hidden")
    
    0 讨论(0)
提交回复
热议问题