简体   繁体   中英

asp.net HiddenField: add custom attribute dynamically

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?

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM