Click here to Skip to main content
15,920,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I am using VS 2005,.net framework 2,asp.net,c#.net
I have a gridview which contains a checkbox in an template column.
I want to check whether maximum one check box should be checked in the grid rows?
Is it possible to do it in Javascipt?


George
Posted

Same Concept i am using Radio Button so you Change it according your way Its may helps to you



XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="AuthId">
            <Columns>
            <asp:TemplateField ShowHeader="false">
            <ItemTemplate>
            <asp:RadioButton ID="rdbauthid" runat="server" onclick="javascript:CheckOtherIsCheckedByGVID(this);" />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="AUTHOR NAME" DataField="AuthName" />
            <asp:BoundField HeaderText="AUTHOR LOCATION" DataField="AuthLocation" />
            </Columns>
        </asp:GridView>




XML
<script type="text/javascript">
            function CheckOtherIsCheckedByGVID(spanChk) {

                var IsChecked = spanChk.checked;
                if (IsChecked) {
                    spanChk.parentElement.parentElement.style.backgroundColor = '#228b22';
                    spanChk.parentElement.parentElement.style.color = 'white';
                }
                var CurrentRdbID = spanChk.id;
                var Chk = spanChk;
                Parent = document.getElementById("<%=GridView1.ClientID%>");
                var items = Parent.getElementsByTagName('input');
                for (i = 0; i < items.length; i++) {
                    if (items[i].id != CurrentRdbID && items[i].type == "radio") {
                        if (items[i].checked) {
                            items[i].checked = false;
                            items[i].parentElement.parentElement.style.backgroundColor = 'white'

                            items[i].parentElement.parentElement.style.color = 'black';
                        }
                    }
                }
            }
</script>
 
Share this answer
 
Hi,
You can use jQuery also.

Something like this
JavaScript
function ValidateCheckboxes(oButton, oEvent){
 if($("input:checked").length==0){
   Alert("Select Any Check Boxes")
   oEvent.cancel = true;
  } }

Here I am calling this in the click of a button. Alter this as your need.

Happy Coding :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900