组名在中继器 asp.net 中的多个单选按钮中不起作用 [英] groupname doesn't work in more than one radiobutton inside repeater asp.net

查看:45
本文介绍了组名在中继器 asp.net 中的多个单选按钮中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个转发器,在转发器内部有一个单选按钮控件,在后面的代码中我填写了单选按钮控件的组名,因此,当我运行它时,我有一个包含多行的表格,其中一些有单选按钮:

I have a repeater and inside the repeater a radiobutton control, in code behind I fill the groupname for the radiobutton control, so, when I run it, I have a table with many rows and some of them have 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>

在repeater的itemdatabound中,我填写了groupname的值:

And in the itemdatabound of repeater I fill the value for 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

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

But when I run it the repeater creates the group name with diferent names:

Radiobutton row 1:
Repeater1$ctl05$1

Radiobutton row 2:

Repeater1$ctl06$1

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

So it let checked all the radiobuttons, instead to uncheck when another one for the same group is cheked.

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

I find this code in a forum, but it work only if I have only one groupname, but I can have more than one groupname:

  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

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

But there can be more than one group of radiobuttons, so in this case I can't use this code.

有什么地方可以做到这一点吗?谢谢

Is there anywhere to do this? thanks

推荐答案

这是一个与 RadioButton 控件在 ItemTemplateAlternatingItemTemplate(更多信息).这是由 Repeater 修改控件 ID 的命名造成的.在后台自动分配的组名称(假设使用动态 ClientIDMode).要解决此问题,请设置客户端功能,如下所示:

This is a known bug related with RadioButton control usage inside ItemTemplate or AlternatingItemTemplate (more info). This caused by Repeater mangling the naming of control ID & group names which assigned automatically in background (assumed using dynamic ClientIDMode). To fix this issue, set up a client-side function like this:

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;
}

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

Later, set the script targeting the radio button control as given below:

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_GroupNameEval 检索 code> 值.或者,您可以使用基本的 HTML input type="radio" 或使用 RadioButtonList 代替.

NB: The first argument of setExclusiveRadioButton method should be set to this regex convention: [repeater control ID].*[RadioButton_GroupName] (RadioButton_GroupName value may be retrieved using Eval). Alternatively you can use basic HTML input type="radio" or use RadioButtonList instead.

参考:

在中继器中使用 RadioButton 控件

类似问题:

中继器内的单选按钮

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

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

这篇关于组名在中继器 asp.net 中的多个单选按钮中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆