为RadioButtonList添加超链接

3
如何在 RadioButtonList 中添加超链接?
我尝试了下面的代码,但它仍然只是纯文本格式。
<asp:RadioButtonList runat="server" id="rdlTest">
    <asp:ListItem Value="1" Text="<a href='Page2.aspx?1'>A</a>"></asp:ListItem>
    <asp:ListItem Value="2" Text="<a href='Page2.aspx?2'>B</a>"></asp:ListItem>
</asp:RadioButtonList>

我知道以下代码可以运行,但我希望使用RadioButtonList而不是RadioButton
<asp:RadioButton runat="server" ID="rdoTest" Text="<a href='Page2.aspx?1'>A</a>" />

更新

我的原始代码没有问题。问题是其他代码干扰了它。 我有一个函数在Page_Load里清除并重新加载rdlTest,使用rdl.Items.Add(new ListItem("1","A"), 这覆盖了有效的标记。


我认为你需要在rowdatabound事件中处理这个问题,通过标记这样做是行不通的。 - JonH
您提供的代码在我的项目中呈现为正确的超链接... - Yannick Blondeau
Ray,你能否发布一下你编辑过的原始代码,以便展示出现错误的代码,以消除任何混淆吗? - MethodMan
2个回答

1

你不能只是做一些像...的事情吗?

<asp:CheckBoxList ID="chk" runat="server">

        <asp:ListItem Value="0"><a href="http://www.asp.net">asp.net</a></asp:ListItem>

        <asp:ListItem Value="1"><a href="http://forums.asp.net">forums.asp.net</a></asp:ListItem>

</asp:CheckBoxList>

RadioButtonList和CheckBoxList应该工作类似。


我尝试使用“RadioButtonList”,但仍然显示为纯文本。 - Ray Cheng
你可能想把以下代码中的<asp:ListItem Value="0"><a href="http://www.asp.net">asp.net</a></asp:ListItem> 改成 <asp:ListItem Value="0">"<a href='http://www.asp.net'>"asp.net</a></asp:ListItem> - MethodMan
@DJKRAZE,我也试过了,但是没有改变任何东西。不过还是谢谢你。 - Ray Cheng
@DJKRAZE,请查看我的更新。原始代码可以工作,但其他东西搞乱了它。 - Ray Cheng

0
你可以给它们分配一个id,然后在Javascript(或jQuery)中绑定一个onclick事件。
<asp:ListItem Value="1" Text="A" ID = "A"></asp:ListItem>

$('#A').click(function(){

window.location='Page2.aspx?1'

}

1
我希望用户通过单击方式选择单选按钮,但仅当他们单击单选按钮的文本部分时,我希望它导航至链接。 - Ray Cheng
尝试将标签放置在ListItem内,并将onclick事件连接到标签。 - Corey Adler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接