向 asp.NET RadioButton 控件添加自定义属性

发布于 2024-09-24 18:42:48 字数 969 浏览 2 评论 0原文

我想向名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪 2024-10-01 18:42:48

我从下面的问题/答案中发现,最简单的方法是通过使用 InputAttributes 属性的代码隐藏,如下所示:

rdoPost.InputAttributes.Add("class", "myCheckBoxClass");

为什么 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:

rdoPost.InputAttributes.Add("class", "myCheckBoxClass");

Why does ASP.Net RadioButton and CheckBox render inside a Span?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文