我有一个repeater,在repeater里面放了一个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>
在重复器的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
但是当我运行它时,repeater会创建不同名称的组名:
Radiobutton row 1:
Repeater1$ctl05$1
Radiobutton row 2:
Repeater1$ctl06$1
因此,当选中同一组的其他单选按钮时,它会将所有单选按钮都选中,而不是取消选中。
我在论坛上找到了这段代码,但仅在我只有一个组名时才起作用,但我可以有多个组名:
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
但是可能会有多组单选按钮,所以在这种情况下我不能使用这段代码。
有没有其他方法可以做到这一点?谢谢。
.each
方法的函数体内使用它们即可。 - Bozhidar Stoyneffif (!IsPostBack)
可能不应该存在,因为这个技巧在后续的 post-back 中停止工作。另外,这个特定的解决方案似乎也可以很好地与AutoPostBack="true"
配合使用。 - esteewhy