我有一个数据绑定的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>
答案 0 :(得分:3)
您可以尝试这样的事情:
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";
但是该属性将在客户端代码中分配和访问。