Telerik
Home / Community / Forums / Grid / CheckBox - only one checked

Not answered CheckBox - only one checked

Feed from this thread
  • DavidA avatar

    Posted on Jul 3, 2009 (permalink)

    Hi,

    I have a datagrid with a CheckBox column. I need this column to only have 1 check box check at any one time. I guess this would be possible through a javascript function? Please can you give me a tip on how to implement this. I have tried using OnCheckedChanged="CheckedChanged" 
    but this is server side. THat is ok, but a client side function would be preferable.

    thanks


                      <telerik:GridTemplateColumn UniqueName="chkEmailAddrField" HeaderText="Email Address Field" HeaderStyle-HorizontalAlign="Center"
                             ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:CheckBox id="chkEmailAddrField" runat="server" OnCheckedChanged="CheckedChanged"  AutoPostBack="True" Checked='<%# DataBinder.Eval( Container, "DataItem.EmailAddrField") %>'></asp:CheckBox>
                        </ItemTemplate>
                     </telerik:GridTemplateColumn>

    thanks
    David (new to controls)

    Reply

  • Shinu MVP Shinu's avatar

    Posted on Jul 5, 2009 (permalink)

    Hi David,

    Try the following client side code for selecting only one checkbox in a grid at any time.

    ASPX:
     
    <ItemTemplate> 
        <asp:CheckBox ID="chkEmailAddrField" runat="server"  onclick="checkboxClicked(event, 'chkEmailAddrField')"
        </asp:CheckBox> 
    </ItemTemplate> 

    JavaScript:
     
    <script type="text/javascript"
    function checkboxClicked(e, idFragment)   
    {   
        var currentCheckBox = e.srcElement || e.target;         
        var inputs = document.getElementsByTagName("input");   
        for (var i = 0; i < inputs.length; i++)   
        {   
            var input = inputs[i];             
            if (input.id == currentCheckBox.id)   
                continue;  
            if (input.id.indexOf(idFragment) < 0)   
                continue;                
            //clear out the rest of the checkboxes   
            if (input.type && input.type == "checkbox")   
            {   
                input.checked = false;   
            }   
        }   
    }  
    </script> 

    Thanks,
    Shinu.

    Reply

Related resourses for "CheckBox - only one checked"

ASP.NET Grid Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  |  Step-by-step Tutorial  ]

Powered by Sitefinity ASP.NET CMS

Contact Us | Site Feedback | Terms of Use | Privacy Policy
Copyright © 2002-2010 Telerik. All rights reserved.