防止在ASP.NET中选择多个单选按钮

时间:2018-03-08 18:33:06

标签: javascript asp.net vb.net

代码:

    <div id="summary_SelectAgent" runat="server">
        <div class="rowClass"><p>Select an Agent:</p></div> 

        <div style="max-height: 200px; overflow: auto;">
        <p><asp:GridView ID="dgvAgents" runat="server" CellPadding="2" AutoGenerateColumns="false" 
            Width="100%" BorderStyle="none" GridLines="None" CssClass="columnfield_small SelectAgentTable">
          <HeaderStyle ForeColor="#3C6994" Font-Underline="False" HorizontalAlign="left"/>
          <Columns>
            <asp:TemplateField ItemStyle-Width="20"><ItemTemplate><asp:RadioButton ID="rbtSelectAgent" runat="server" 
              AutoPostBack="false" CausesValidation="False" GroupName="SelectAgent"/></ItemTemplate></asp:TemplateField>
            <asp:BoundField DataField="cName" HeaderText="Agent Name" HeaderStyle-HorizontalAlign="Left" />
            <asp:BoundField DataField="cAddress" HeaderText="Address" HeaderStyle-HorizontalAlign="Left" />
            <asp:BoundField DataField="cCityStZip" HeaderText="City/State/Zip" HeaderStyle-HorizontalAlign="Left" />
            <asp:BoundField DataField="nIDNumber" HeaderText="#" HeaderStyle-HorizontalAlign="Left" Visible="false"/>
          </Columns>
        </asp:GridView></p>
        </div>
        <div class="rowClass"></div>
    </div>

我觉得我需要在radiobutton区域添加一个JavaScript事件。

1 个答案:

答案 0 :(得分:0)

由于您在网格视图中使用单选按钮,因此必须在单选按钮的GroupName事件中设置OnInit

<asp:RadioButton ID="test" runat="server" OnInit="test_Init" />

现在你的代码落后于

protected void test_Init(object sender, EventArgs e)
        {
            RadioButton rbtn = (RadioButton)sender;
            rbtn.GroupName = "groupgrid";
        }