嵌套在gridview中checkbox实现单选、全选功能(js实现)

1.html

//实现单选功能
  <script type="text/javascript"> 

var ClickNum=0;//判断同一checkbox连续点击次数

   var PreCheckboxID="";//记录点击checkbox的ID

   //listId为控制checkbox的范围

   //SelectRadio是点击checkbox自己

function SignCheck(listId,SelectCheckBox)

{

   ///判断是否连击同一个checkbox

    if(PreCheckboxID==SelectCheckBox.id)

    {

        ClickNum = ClickNum + 1;

    }

    else

    {

        PreCheckboxID = SelectCheckBox.id;

        ClickNum = 0;

    }

    //找到控制范围

   var GridViewableSearchList=document .getElementById(listId);

   //找到控制范围下所有input

   var objs=GridViewableSearchList.getElementsByTagName("input");

   //找到控制范围下所有checkbox并都变为false

    for(var i = 0; i < objs.length; i++) 

    {

        if(objs[i].type.toLowerCase() == "checkbox" )

        objs[i].checked = false;

    }

    var SelectCheckBoxId=SelectCheckBox.id; 

    //如果连击次数为奇次checked为true偶次为false

    if(ClickNum%2!=1)

    {

       document.getElementById(SelectCheckBoxId).checked = true;

    }

    else

    {

        document.getElementById(SelectCheckBoxId).checked = false;

    }

}

//全选、全不选功能实现
function GetAllCheckBox(CheckAll)
{
var items=document.getElementsByTagName("input");
for(var i=0;i<items.length;i++)
{
if(items[i].type=="checkbox")
{

items[i].checked=CheckAll.checked;
}

}
}
    </script>

2.asp.net

 <asp:GridView ID="gv_customer" runat="server" Width="100%" CellPadding="4" EmptyDataText="没有任何数据!"
                                        AutoGenerateColumns="False" BackColor="Silver" BorderWidth="2px" CellSpacing="1"
                                        GridLines="None" DataKeyNames="id" AllowPaging="True" OnPageIndexChanging="gv_customer_PageIndexChanging">
                                        <HeaderStyle BackColor="#EAF2F5" Font-Bold="True" HorizontalAlign="Center" Height="30px" />
                                        <RowStyle BackColor="White" Height="30px" />
                                        <EmptyDataRowStyle BackColor="White" HorizontalAlign="Center" VerticalAlign="Middle"
                                            BorderColor="White" BorderStyle="None" />
                                        <Columns>
                                            <asp:CommandField ShowSelectButton="True" Visible="False" />
                                            <asp:TemplateField HeaderText="选择">
                                                <ItemTemplate>
                                                    <asp:CheckBox ID="CheckBox2" runat="server" onclick="SignCheck('DivGridview',this);" />
                                                </ItemTemplate>
                                                <ItemStyle Width="30px" />
                                                <HeaderTemplate>
                                                    <input type="checkbox" id="CheckBox1" name="CheckBox1" onclick="GetAllCheckBox(this)" /><%--全选--%>
                                                </HeaderTemplate>
                                            </asp:TemplateField>
                                       <asp:TemplateField HeaderText="序号">
                                   
                                        <ItemTemplate>
                                            <a href="#" onclick="window.parent.frames('right').window.location='customer_abnormal_ed.aspx?id=<%#Eval("id") %>'">
                                            <asp:Label ID="Label1" runat="server" Text="<%# Container.DataItemIndex + 1 %>"></asp:Label></a>
                                        </ItemTemplate>
                                          <ItemStyle Width="30px" HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                            <asp:BoundField DataField="area" HeaderText="区域">
                                                <ItemStyle Width="30px" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="code" HeaderText="客户编号">
                                                <ItemStyle Width="60px" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="name" HeaderText="客户名称">
                                                <ItemStyle Width="110px" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="content" HeaderText="情况描述">
                                                <ItemStyle Width="250px" HorizontalAlign="Left" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="SetMan" HeaderText="反馈人">
                                                <ItemStyle Width="60px" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="SetTime" HeaderText="反馈时间" HtmlEncode="False" ApplyFormatInEditMode="True"
                                                DataFormatString="{0:d}  ">
                                                <ItemStyle Width="80px" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="HandleMan" HeaderText="处理人">
                                                <ItemStyle Width="80px" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="HandleTime" HeaderText="处理时间" DataFormatString="{0:d}  "
                                                HtmlEncode="False" ApplyFormatInEditMode="True">
                                                <ItemStyle Width="80px" />
                                            </asp:BoundField>
                                            <asp:TemplateField HeaderText="处理">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label3" runat="server" Text='<%#(string)Eval("state")=="1"?"是":"否" %>'><%#Eval("state") %>></asp:Label>
                                                </ItemTemplate>
                                                <ItemStyle Width="30px" />
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="审核">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label4" runat="server" Text='<%#(string)Eval("check_state")=="1"?"是":"否" %>'><%#Eval("check_state") %>></asp:Label>
                                                </ItemTemplate>
                                                <ItemStyle Width="30px" />
                                            </asp:TemplateField>
                                            <asp:BoundField />
                                        </Columns>
                                        <EmptyDataTemplate>
                                            目前没有任何数据</EmptyDataTemplate>
                                        <PagerTemplate>
                                            <asp:LinkButton ID="cmdFirstPage" runat="server" CommandName="Page" CommandArgument="First"
                                                Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">首页</asp:LinkButton>
                                            <asp:LinkButton ID="cmdPreview" runat="server" CommandArgument="Prev" CommandName="Page"
                                                Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">前页</asp:LinkButton><asp:Label ID="lblcurPage" ForeColor="Blue" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1  %>'></asp:Label>页/共<asp:Label
                                                ID="lblPageCount" ForeColor="blue" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></asp:Label><asp:LinkButton ID="cmdNext" runat="server" CommandName="Page" CommandArgument="Next"
                                                Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">后页</asp:LinkButton>
                                            <asp:LinkButton ID="cmdLastPage" runat="server" CommandArgument="Last" CommandName="Page"
                                                Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">尾页</asp:LinkButton>
                                            &nbsp;
                                            <div>
                                            </div>
                                        </PagerTemplate>
                                        <PagerStyle BackColor="#F2FAFE" ForeColor="Black" HorizontalAlign="Center" />
                                        <SelectedRowStyle BackColor="#EAF8FF" Font-Bold="True" />
                                        <AlternatingRowStyle BackColor="White" />
                                    </asp:GridView>
                               

 

 

posted on 2013-06-08 09:20  yjheimi  阅读(400)  评论(0编辑  收藏  举报