向 asp.NET RadioButton 控件添加自定义属性
我想向名为 Key
的 asp.net RadioButton 添加一个自定义属性,我正在使用客户端进行 ajax 请求。
我发现我的 aspx 标记如下:
<asp:RadioButton ID="rdoPost" GroupName=PreferredContactMethod" value="Post" onclick="DoStuff(this)" runat="server" />
在页面中呈现,
<span Key="ContactMethod">
<input id="rdoPost" type="radio" name="PreferredContactMethod"" value="Post" onclick="DoStuff(this);" />
</span>
而我期望(并希望)得到以下内容
<input id="rdoPost" type="radio" Key="ContactMethod" name="PreferredContactMethod"" value="Post" onclick="DoStuff(this);" />
我已经使用 asp TextBox 控件尝试了相同的操作,并且它完全正常工作正如我所期望的,只需将 Key="myKey"
属性添加到 元素即可。
有没有一种方法可以使用标准 RadioButton 控件来解决这个问题,或者我是否必须从标准控件继承才能实现我想要的标记?
另外...(抱歉同时问两个问题),向 html 标记添加非标准属性是一个坏主意吗?目前,我通过以下方式在 JavaScript 中使用这些属性:
var key = rdoPost.Key;
I want to add a custom attribute to an asp.net RadioButton called Key
which I'm using client-side for an ajax request.
What I'm finding is that my aspx markup which is the following:
<asp:RadioButton ID="rdoPost" GroupName=PreferredContactMethod" value="Post" onclick="DoStuff(this)" runat="server" />
gets rendered in the page as
<span Key="ContactMethod">
<input id="rdoPost" type="radio" name="PreferredContactMethod"" value="Post" onclick="DoStuff(this);" />
</span>
whereas I'd expected (and hoped) to get the following
<input id="rdoPost" type="radio" Key="ContactMethod" name="PreferredContactMethod"" value="Post" onclick="DoStuff(this);" />
I've tried the same thing with an asp TextBox control and it works exactly as I'd expect simply adding the Key="myKey"
attribute to the <input type="text"/>
element.
Is there a way around this with the standard RadioButton control, or will I have to inherit from the standard one to achieve the markup I'm wanting?
Also... (sorry to ask two questions at the same time), is adding non-standard attributes to html markup a bad idea anyway? Currently I'm using these attributes in JavaScript in the following way:
var key = rdoPost.Key;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从下面的问题/答案中发现,最简单的方法是通过使用
InputAttributes
属性的代码隐藏,如下所示:为什么 ASP.Net RadioButton 和 CheckBox 在 Span 内呈现?
I've found from the question/answer below that the easiest way to do this is via the code-behind using the
InputAttributes
property as follows:Why does ASP.Net RadioButton and CheckBox render inside a Span?