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

Esr*_*_92 10 c# vb.net asp.net repeater radio-button

我有一个转发器,在转发器内部有一个radiobutton控件,在后面的代码中我填充了radiobutton控件的groupname,因此,当我运行它时,我有一个包含许多行的表,其中一些有radiobutton:

  <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>
Run Code Online (Sandbox Code Playgroud)

在转发器的itemdatabound中,我填充groupname的值:

  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
Run Code Online (Sandbox Code Playgroud)

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

Radiobutton row 1:
Repeater1$ctl05$1

Radiobutton row 2:

Repeater1$ctl06$1
Run Code Online (Sandbox Code Playgroud)

所以它让我们检查所有的radiobuttons,而不是当同一组中的另一个被解雇时取消选中.

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

  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
Run Code Online (Sandbox Code Playgroud)

但是可以有多组radiobuttons,所以在这种情况下我不能使用这个代码.

有没有可以做到这一点?谢谢

Tet*_*oto 10

这是与RadioButton内部控件使用相关的已知错误ItemTemplateAlternatingItemTemplate(更多信息).这是由于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;
}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

参考:

在Repeater中使用RadioButton控件

类似问题:

中继器内的radiobutton

在中继器中只选择一个单选按钮

ASP.NET - 中继器中的单选按钮


Web*_*ter 5

由于其他用户提供了问题的根本原因,所以我不会解释相同,但我会为您提供 基于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")); 
            });
Run Code Online (Sandbox Code Playgroud)

attr("name",jQuery("[name$='optValue']")将尽量选择与结束在页面上所有的投入optValue,即optValue在转发项目.之后,它将name属性更改为为所有optValue元素找到的第一个值.该attr("name")函数(此处以'get'格式使用)始终返回列表中的第一个.因此,所有选项按钮在Html中都获得相同的"名称"属性,这使得select可以正常工作.

这个来源的一个很好的解决方案