Click here to Skip to main content
16,016,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a javascript which check for checkbox limit if the user select more then 3 checkbox. I can't find the onClick event for the gridview to attach my C# codings for it.

<script type="text/javascript">
            function chkCount(obj) {
                if (obj.checked == true)
                {
                    if (document.getElementById('<%=hiddenChkCount.ClientID %>').value >= 3)
                    {
                    alert('You cannot select more than 3 items.');
                    obj.checked = false;
                    }
                    else
                    {
                        document.getElementById('<%=hiddenChkCount.ClientID %>').value = parseInt(document.getElementById('<%=hiddenChkCount.ClientID %>').value) + 1;
                    }
                }
                else
                {
                    document.getElementById('<%=hiddenChkCount.ClientID %>').value = parseInt(document.getElementById('<%=hiddenChkCount.ClientID %>').value) - 1;
                }
            }
             </script>


Current Code without a "home"

protected void checkboxCOUNT(object sender, EventArgs e)
        {
            CheckBox chk;
            foreach (GridViewRow rowItem in Gv_Children.Rows)
            {
                // get reference to checkbox control placed inside the first cell of gridview control
                chk = (CheckBox)(rowItem.Cells[0].FindControl("chkSelect"));
                chk.Attributes.Add("onclick", "chkCount(this)");
            }
        }



GridView Code

<asp:GridView ID="Gv_Children" runat="server" AllowPaging="True"
                                AllowSorting="True" AutoGenerateColumns="False" BackColor="#DEBA84"
                                BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
                                CellSpacing="2" DataSourceID="DataSourceGuardian"
                                ShowHeaderWhenEmpty="True"
                                style="margin-right: 1px" ViewStateMode="Enabled" Width="255px"
                                >
                                <Columns>
                                    <asp:BoundField DataField="GuardianIC" HeaderText="IC" ReadOnly="True"
                                        SortExpression="GuardianIC" />
                                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                                    <asp:TemplateField HeaderText="Select">
                                            <ItemTemplate>
                                                 <asp:CheckBox ID="chkSelect" runat="server" />
                                            </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <EmptyDataRowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                                <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                                <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                                <SortedAscendingCellStyle BackColor="#FFF1D4" />
                                <SortedAscendingHeaderStyle BackColor="#B95C30" />
                                <SortedDescendingCellStyle BackColor="#F1E5CE" />
                                <SortedDescendingHeaderStyle BackColor="#93451F" />
                            </asp:GridView
>
Posted
Updated 15-Nov-10 23:00pm
v2

1 solution

you don't want to handle the event on the gridview, instead handle the event on the checkbox:

<asp:CheckBox ID="chkSelect" runat="server" onClick="SomeJSFunction();" />
 
Share this answer
 
Comments
Royaltyming 17-Nov-10 23:09pm    
I've tried the code before. It does not work somehow. Is it related to the Javascript or my checkbox events?

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