groupname 在中继器内的多个单选按钮中不起作用 asp.net

groupname doesn't work in more than one radiobutton inside repeater asp.net

我有一个中继器,在中继器内部有一个单选按钮控件,在后面的代码中,我为单选按钮控件填充了组名,所以,当我 运行 它时,我有一个 table 有很多行,其中一些有单选按钮:

  <asp:updatepanel id="UpdatePanel1" runat="server" updatemode="Conditional">
    <ContentTemplate>
        <asp:Repeater ID="Repeater1" runat="server" ViewStateMode="Enabled">
            <HeaderTemplate>
                <table class="table table-responsive table-bordered ">
                    <tr class="text-center" style="background-color: #6e6259; color: white;">
                        <th class="text-center">DESCRIPTION</th>
</HeaderTemplate>
            <ItemTemplate>
         <tr>
        <td style="padding-left: 20px;">
      <asp:RadioButton ID="rbtDinamic"  OnCheckedChanged="rbtDinamic_CheckedChanged" AutoPostBack="true"
           ViewStateMode="Enabled" Visible="false"  GroupName='<%#Eval("groupvalue") %>'   runat="server"/></td>
</ItemTemplate>
      <FooterTemplate>
    </table>
     </FooterTemplate>
    </asp:Repeater>
     </ContentTemplate>
      </asp:UpdatePanel>

并且在转发器的 itemdatabound 中,我填写了组名的值:

  Private Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    Try
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            If CType(e.Item.FindControl("hdf1"), Label).Text = False Then
                CType(e.Item.FindControl("rbtDinamic"), RadioButton).Visible = True
                CType(e.Item.FindControl("rbtDinamic"), RadioButton).GroupName = CType(e.Item.FindControl("groupvalue"), Label).Text = False
            End If
        End If
    Catch ex As Exception          
    End Try
End Sub

但是当我 运行 它时,中继器会创建具有不同名称的组名称:

Radiobutton row 1:
Repeater1$ctl05

Radiobutton row 2:

Repeater1$ctl06

所以它让选中所有单选按钮,而不是在选中同一组的另一个单选按钮时取消选中。

我在论坛上找到了这段代码,但它只有在我只有一个组名时才有效,但我可以有多个组名:

  Protected Sub rbtDinamic_CheckedChanged(sender As Object, e As EventArgs)
    For Each item As RepeaterItem In Repeater1.Items
        Dim rbtn As RadioButton = DirectCast(item.FindControl("rbtDinamic"), RadioButton)
        rbtn.Checked = False
    Next
    DirectCast(sender, RadioButton).Checked = True
End Sub

但是可以有不止一组单选按钮,所以在这种情况下我不能使用这段代码。

有什么地方可以做这个吗?谢谢

这是一个已知错误,与 RadioButton 控件在 ItemTemplateAlternatingItemTemplate (more info) 中的使用有关。这是由于 Repeater 破坏了在后台自动分配的控件 ID 和组名称的命名(假设使用动态 ClientIDMode)。要解决此问题,请像这样设置客户端功能:

function setExclusiveRadioButton(name, current)
{
    regex = new RegExp(name);  

    for (i = 0; i < document.forms[0].elements.length; i++)
    {
        var elem = document.forms[0].elements[i];
        if (elem.type == 'radio')
        {
           elem.checked = false;
        }
    }
    current.checked = true;
}

稍后,设置针对单选按钮控件的脚本,如下所示:

Private Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    Try
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            If CType(e.Item.FindControl("hdf1"), Label).Text = False Then
                CType(e.Item.FindControl("rbtDinamic"), RadioButton).Visible = True
                CType(e.Item.FindControl("rbtDinamic"), RadioButton).GroupName = CType(e.Item.FindControl("groupvalue"), Label).Text = False
            End If
        End If

        ' put the proper client-side handler for RadioButton
        Dim radio As RadioButton = CType(e.Item.FindControl("rbtDinamic"), RadioButton)
        Dim script As String = "setExclusiveRadioButton('Repeater1.*[RadioButton_GroupName]', this)"

        radio.Attributes.Add("onclick", script)

    Catch ex As Exception          
    End Try
End Sub

注意:setExclusiveRadioButton 方法的第一个参数应该设置为这个正则表达式约定:[repeater control ID].*[RadioButton_GroupName]RadioButton_GroupName 值可以使用 Eval 检索)。或者,您可以使用基本 HTML input type="radio" 或使用 RadioButtonList

参考:

Using RadioButton Controls in a Repeater

类似问题:

radiobutton inside repeater

only one radiobutton selection in repeater

ASP.NET - Radio Buttons In Repeaters

由于其他用户提供了问题的根本原因,所以我不会解释相同但我会为您提供基于 Jquery 的解决方案:

jQuery("[name$='$optValue']").attr("name",jQuery("[name$='$optValue']").attr("name"));

jQuery("[name$='$optValue]").click(function (){ 
                //set name for all to name of clicked 
                jQuery("[name$='$optValue]").attr("name", this.attr("name")); 
            });

with attr("name",jQuery("[name$='optValue']") 将尝试 select 页面上以 optValue 结尾的所有输入,即转发器中的 optValue 项。之后,它将 name 属性更改为为所有 optValue 元素找到的第一个值。 attr("name") 函数(此处以 'get' 格式使用)总是 returns 列表中的第一个。所以所有的选项按钮在 Html 中获得相同的 'name' 属性,这使得 select 可以正常工作。

来自 this Source

的出色变通方法

在客户端,将无线电的 name 设置为您喜欢的任何组,但在 data- 属性中写下生成的名称。

然后,在提交表单之前,将 data- 属性复制回 name 属性,以便 ASP.NET 可以识别服务器上的控件。

这个脚本可以做到:

<script type="text/javascript">
    $(document).ready(function (e) {            
        $("input[type='radio']").each(function (idx, elm) {
            var generatedName = $(elm).attr("name");
            $(elm).data("name", generatedName);
            $(elm).attr("name", "whatever-group-name");
        });

    });

    function onSubmit() {
        $("input[type='radio']").each(function (idx, elm) {
            var generatedName = $(elm).data("name");
            $(elm).attr("name", generatedName);
        });
    }
</script>

要检测表单提交,您需要调用 RegisterOnSubmitStatement。例如。在你的 Page_Load:

if (!IsPostBack)
{
    Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "prepareSubmit", "onSubmit();");
}