问题描述
我想向名为 Key
的asp.net单选按钮添加自定义属性,我正在使用客户端的ajax请求.
I want to add a custom attribute to an asp.net RadioButton called Key
which I'm using client-side for an ajax request.
我发现的是我的aspx标记,如下所示:
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" />
在页面中呈现为
<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);" />
我已经尝试过使用asp TextBox控件进行相同的操作,并且它完全可以按我期望的那样工作,只需将 Key ="myKey"
属性添加到< input type ="text"/>
元素.
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.
使用标准RadioButton控件是否可以解决此问题,还是必须继承自标准按钮才能实现所需的标记?
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?
也...(很抱歉同时问两个问题),将非标准属性添加到html标记中还是个坏主意吗?目前,我正在通过以下方式在JavaScript中使用这些属性:
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;
推荐答案
我从下面的问题/答案中发现,最简单的方法是使用 InputAttributes
属性如下:
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:
rdoPost.InputAttributes.Add("class", "myCheckBoxClass");
为什么ASP.NetRadioButton和CheckBox在Span内渲染?
这篇关于向ASP.NET RadioButton控件添加自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!