我有一个asp.net RadioButtonList是数据绑定(bind)的。呈现的列表项显然呈现为 radio 类型,标签和跨度的输入。
当我遍历每个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/