9

How can I add a custom attribute to a HiddenValueField in ASP.NET? Specifically, I need a class="gmapPoint" attribute for a dynamically generated HiddenValue control. This is necessary for a JavaScript on that page

var hiddenField = new HiddenField();
hfield.Value = "myValue";
hfield.... Attributes["class"]

-- how can I do a similar thing?

lekso
  • 1,731
  • 3
  • 24
  • 46

1 Answers1

21

Use HtmlInputHidden control instead of HiddenField. It allows programmatic access to the HTML <input type=hidden> element on the server and has .Attributes property:

var hiddenField = new HtmlInputHidden();
hiddenField.Value = "myValue";
hiddenField.Attributes["class"] = "a-class-for-a-hidden-field";
Oleks
  • 31,955
  • 11
  • 77
  • 132