Search code examples
c#asp.netgridviewcheckboxtemplatefield

How to allow only one checked box in the gridview?


If i have a grid view contains checkbox chk_short_day as template field :

<asp:GridView ID="grv_week_day" runat="server" AutoGenerateColumns="False" 
     CssClass="datatable" OnRowDataBound="grv_week_day_RowDataBound" >
<Columns>
    <asp:TemplateField HeaderStyle-Height="40px" HeaderStyle-Width="200px">
        <HeaderTemplate>
            <h2>week days
            </h2>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Label ID="lbl_weekday" runat="server" CssClass="title" Text='<%# Bind("WeekDay") %>'></asp:Label>
        </ItemTemplate>
        <HeaderStyle Height="40px" Width="100px"></HeaderStyle>
    </asp:TemplateField>
    <asp:TemplateField HeaderStyle-Height="40px">
        <HeaderTemplate>
            <h2>Attendance Type</h2>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:DropDownList ID="drp_att" runat="server" AutoPostBack="True" Width="200px" Enabled="false"
                CausesValidation="false" OnSelectedIndexChanged="OnSelectedIndexChanged_drp">
            </asp:DropDownList>
        </ItemTemplate>
        <HeaderStyle Height="40px"></HeaderStyle>
    </asp:TemplateField>
    <asp:TemplateField HeaderStyle-Height="40px">
        <HeaderTemplate>
            <h2>From
            </h2>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Label ID="lbl_From" runat="server"></asp:Label>
        </ItemTemplate>
        <HeaderStyle Height="40px"></HeaderStyle>
    </asp:TemplateField>
    <asp:TemplateField HeaderStyle-Height="40px">
        <HeaderTemplate>
            <h2>To
            </h2>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Label ID="lbl_To" runat="server"></asp:Label>
        </ItemTemplate>
        <HeaderStyle Height="40px"></HeaderStyle>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Short Day">
        <ItemTemplate>
            <asp:CheckBox ID="chk_short_day" runat="server" AutoPostBack ="true" OnCheckedChanged="chk_short_day_CheckedChanged"   />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>


How to allow only one checked in the gridview , If the user check one of the check boxes ,i want to uncheck the rest automatically .?


Solution

  • How about using a radio button instead? Only one radio button within the same Group are allowed to be selected.

    <asp:TemplateField HeaderText="Short Day">
       <ItemTemplate>
           <asp:RadioButton id="rbt_short_day" GroupName="shortDay"/>
       </ItemTemplate>
    </asp:TemplateField>