我使用Repeater控件在ASP.NET web表单上显示一系列照片。以下是我的当前代码:
<asp:Repeater ID="repPhotos" runat="server">
<ItemTemplate>
<asp:hyperlink id="link" NavigateUrl='<%# Container.DataItem %>' runat="server">
<asp:Image ID="Image" runat="server" ImageUrl='<%# Container.DataItem %>' Height="10%" Width="10%" />
</asp:hyperlink>
</ItemTemplate>
</asp:Repeater>现在我想在每一张照片下面显示一个无线电按钮,但是一系列的无线电按钮必须是相互排斥的。我尝试使用ASP:RadioButton控件,但在阻止同时选择多个无线电按钮时遇到了困难,我不确定ASP:RadioButtonList如何与Repeater一起工作。
感谢您的建议!
发布于 2016-10-13 15:23:31
不幸的是,如果将RadioButton的GroupName放置在单个行中,则它们在Repeater或GridView中无法工作。但是,您可以使用几行jQuery轻松地实现它。
function radioButtonClick(sender) {
$('.myRadioButton input').attr("checked", false);
$(sender).prop("checked", true);
}单击单选按钮时,取消选中所有具有myRadioButton类名的单选按钮。然后,只检查一个触发单击事件。
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DemoWebForm.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="repPhotos" runat="server">
<ItemTemplate>
<asp:RadioButton runat="server" ID="PhotoRadioButton"
CssClass="myRadioButton" onclick="radioButtonClick(this)" />
<%# Container.DataItem %>
</ItemTemplate>
</asp:Repeater>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
function radioButtonClick(sender) {
$('.myRadioButton input').attr("checked", false);
$(sender).prop("checked", true);
}
</script>
</form>
</body>
</html>代码背后
using System;
using System.Collections.Generic;
namespace DemoWebForm
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
repPhotos.DataSource = new List<string> {"One", "Two", "Three"};
repPhotos.DataBind();
}
}
}https://stackoverflow.com/questions/40023551
复制相似问题