我有一个数据绑定(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/