博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

GridView 中的checkbox加上选择所有的功能

Posted on 2008-07-24 09:39  LonelyStar  阅读(429)  评论(0编辑  收藏  举报

在GridView外面加上一个checkbox

 

1<input id="Checkbox1" type="checkbox" title="Check/Uncheck All" onclick="SelectAllPassenger(this);" />&nbsp;Check/Uncheck All

 

 

 

 1<asp:GridView ID="GridViewPassenger" runat="server" AutoGenerateColumns="False" EmptyDataText="No Passengers" Width="100%">
 2                        <Columns>
 3                            <asp:TemplateField>
 4                                <ItemTemplate>
 5                                    <input name="CheckboxPassenger" type="checkbox" value='<%#Eval("PassengerId") %>' />
 6                                </ItemTemplate>
 7                            </asp:TemplateField>
 8                            <asp:BoundField HeaderText="Pick Up" DataField="GetOn" />
 9                            <asp:BoundField HeaderText="Drop Off" DataField="GetOff" />
10                            <asp:BoundField HeaderText="Name" DataField="Name" />
11                            <asp:BoundField HeaderText="Mobile" DataField="Mobile" />
12                            <asp:BoundField HeaderText="Email" DataField="Email" />
13                        </Columns>
14                    </asp:GridView>

 

 

 1<script language="javascript">
 2unction SelectAllPassenger(chkall)
 3        {
 4            //get reference of GridView control
 5            var grid = document.getElementById("<%= GridViewPassenger.ClientID %>");
 6            var inputs = grid.getElementsByTagName("input");
 7
 8            for (var i=0; i<inputs.length; i++)
 9            {
10                //if childNode type is CheckBox                 
11                if (inputs[i].type =="checkbox" && inputs[i].disabled == false)
12                {
13                   //assign the status of the Select All checkbox to the cell checkbox within the grid
14                    inputs[i].checked = chkall.checked;
15                }

16            }

17        }
 
18  </script>