html - 如何将属性添加到输入标签 asp.net 内的 RadioButtonList 项目

标签 html asp.net vb.net radiobuttonlist custom-attribute

我有一个数据绑定(bind)的 asp.net RadioButtonList。呈现的列表项显然呈现为单选类型的输入、标签和跨度。

当我遍历每个 ListItem 并添加一个 onclick 属性时,它会根据需要将 onclick 属性添加到输入标签。但是当我添加一个自定义属性时,它会将它添加到周围的跨度中。如何在不通过自定义 ControlAdapter 更改 RadioButtonList 的呈现的情况下将其添加到输入标签?我正在使用网站(不是项目)和 .net 2.0。提前致谢!

ASP.NET

For Each li As ListItem In Me.rbl.Items
   li.Attributes.Add("onclick", "myFunction();")
   li.Attributes.Add("myAttribute", "1")
Next

HTML

<table id="ctl00_ContentPlaceHolder1_rbl" border="0">
    <tr>
        <td><span myAttribute="1"><input id="ctl00_ContentPlaceHolder1_rbl_0" type="radio"
          name="ctl00$ContentPlaceHolder1$rbl" value="Choice1" onclick="myFunction();" />
         <label for="ctl00_ContentPlaceHolder1_rbl_0">Choice1</label></span></td>
    </tr>
</table>

最佳答案

你可以尝试这样的事情:

Dim i As Integer = 0

For Each li As ListItem In Me.rbl.Items
   li.Attributes.Add("onclick", "myFunction();")
   ClientScript.RegisterExpandoAttribute(rbl.ClientID & "_" & i.ToString, "myAttribute", "1")
   i += 1
Next

属性在 HTML 中是不可见的,因为在上面添加的行会生成一个客户端脚本,类似于

var ctl00_ContentPlaceHolder1_rbl_0 = document.all ? document.all["ctl00_ContentPlaceHolder1_rbl_0"] : document.getElementById("ctl00_ContentPlaceHolder1_rbl_0");
ctl00_ContentPlaceHolder1_rbl_0.myAttribute = "1";
var ctl00_ContentPlaceHolder1_rbl_1 = document.all ? document.all["ctl00_ContentPlaceHolder1_rbl_1"] : document.getElementById("ctl00_ContentPlaceHolder1_rbl_1");
ctl00_ContentPlaceHolder1_rbl_1.myAttribute = "1";

但该属性将在客户端代码中分配和访问。

关于html - 如何将属性添加到输入标签 asp.net 内的 RadioButtonList 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25065710/

相关文章:

javascript - 如何在 IE 中创建一个新的 HTMLDocument?

mysql - 使用 MVC Asp.net 删除 MySql 中的数据超时

c# - 使用 ashx 用 html 填充 div

c# - 升级到 EL 5 'Microsoft.Practices.EnterpriseLibrary.Caching ..或其依赖项之一时出错

vb.net - Visual Studio 2010 的 VB.NET 代码编辑器中的方法下拉列表是否有键盘快捷键?

javascript - 图像 src 无法通过 JavaScript 工作

javascript - 导航列表中的事件链接

Javascript向具有相同类的元素添加样式

javascript - 修改jQuery函数(抓取每个DOM元素)

.net - 对象不为空的排序列表