[英]asp.net HiddenField: add custom attribute dynamically
How can I add a custom attribute to a HiddenValueField in ASP.NET? 如何在ASP.NET中向HiddenValueField添加自定义属性? Specifically, I need a class="gmapPoint" attribute for a dynamically generated HiddenValue control.
具体来说,我需要一个class =“gmapPoint”属性来动态生成HiddenValue控件。 This is necessary for a JavaScript on that page
这对于该页面上的JavaScript是必需的
var hiddenField = new HiddenField();
hfield.Value = "myValue";
hfield.... Attributes["class"]
-- how can I do a similar thing? - 我怎么能做类似的事情?
Use HtmlInputHidden
control instead of HiddenField
. 使用
HtmlInputHidden
控件而不是HiddenField
。 It allows programmatic access to the HTML <input type=hidden>
element on the server and has .Attributes
property: 它允许以编程方式访问服务器上的HTML
<input type=hidden>
元素,并具有.Attributes
属性:
var hiddenField = new HtmlInputHidden();
hiddenField.Value = "myValue";
hiddenField.Attributes["class"] = "a-class-for-a-hidden-field";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.