多个面板内的Radiobutton

时间:2016-02-10 08:10:48

标签: asp.net radio-button radiobuttonlist

我需要在3个面板中放置3个radiobutton,然后根据用户登录隐藏。现在的问题是所有的radiobutton都可见。我不能让用户只勾选一个。我尝试了Groupname属性,但没有找到运气。

                                              <td>
                                                  <asp:Panel ID="Panel100" runat="server" Visible="True" >
                                                      <asp:RadioButtonList ID="cpem13" runat="server" CellPadding="0" CellSpacing="0" Font-Size="X-Small" Height="18px" RepeatDirection="Horizontal" Width="74px" GroupName="B1" >
                                                          <asp:ListItem Text="PDA/PPN" Value="5"></asp:ListItem>
                                                      </asp:RadioButtonList>
                                                  </asp:Panel> 
                                              </td>
                                              <td>
                                                  <asp:Panel ID="Panel102" runat="server" Visible="True" >
                                                      <asp:RadioButtonList ID="RadioButtonList1" runat="server" CellPadding="0" CellSpacing="0" Height="18px" RepeatDirection="Horizontal" Width="80px" Font-Size="X-Small" GroupName="B1">
                                                          <asp:ListItem Text="PPP/PPW" Value="0"></asp:ListItem>
                                                      </asp:RadioButtonList> 
                                                  </asp:Panel> </td>
                                              <td>
                                                  <asp:Panel ID="Panel103" runat="server" Visible="True" Width="68px" >
                                                      <asp:RadioButtonList ID="RadioButtonList2" runat="server" CellPadding="0" CellSpacing="0" Height="16px" RepeatDirection="Horizontal" Width="218px" Font-Size="X-Small" GroupName="B1">
                                                            <asp:ListItem Text="PAE" Value="1"></asp:ListItem>
                                                        </asp:RadioButtonList> 
                                                  </asp:Panel> </td>

1 个答案:

答案 0 :(得分:1)

  <td>
        <asp:panel id="Panel100" runat="server" visible="True">
            <asp:RadioButtonList AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" runat="server"  CellPadding="0" CellSpacing="0" Font-Size="X-Small" Height="18px" RepeatDirection="Horizontal" Width="74px"  >
                <asp:ListItem Text="PDA/PPN" Value="5"></asp:ListItem>
            </asp:RadioButtonList>
        </asp:panel>
    </td>
    <td>
        <asp:panel id="Panel102" runat="server" visible="True">
            <asp:RadioButtonList AutoPostBack="true" OnSelectedIndexChanged ="RadioButtonList2_SelectedIndexChanged" ID="RadioButtonList2"  runat="server" CellPadding="0" CellSpacing="0" Height="18px" RepeatDirection="Horizontal" Width="80px" Font-Size="X-Small" >
                <asp:ListItem Text="PPP/PPW" Value="0"></asp:ListItem>
            </asp:RadioButtonList> 
        </asp:panel>
    </td>
    <td>
        <asp:panel id="Panel103" runat="server" visible="True" width="68px">
            <asp:RadioButtonList AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList3_SelectedIndexChanged" ID="RadioButtonList3"  runat="server" CellPadding="0" CellSpacing="0" Height="16px" RepeatDirection="Horizontal" Width="218px" Font-Size="X-Small">
                <asp:ListItem Text="PAE" Value="1"></asp:ListItem>
            </asp:RadioButtonList> 
        </asp:panel>
    </td>

并在代码中

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList2.SelectedIndex = -1;
        RadioButtonList3.SelectedIndex = -1;

    }
    protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList1.SelectedIndex = -1;
        RadioButtonList3.SelectedIndex = -1;
    }
    protected void RadioButtonList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList1.SelectedIndex = -1;
        RadioButtonList2.SelectedIndex = -1;
    }

您可以使用“OnSelectedIndexChanged”事件来实现此