• GridView中CheckBox的用法


    以下是我在GridView中控制CheckBox的全选/取消全选,根据子CheckBox处理全选CheckBox状态的操作并获取所选GridView中所选行的列的值。
    脚本代码:
        <script language="javascript" type="text/javascript">
        //全选/全取消GridView中的CheckBox
        //参数gv:  GridView的ID
        //参数input2: 功能控件ID(全选CheckBox)
        function CheckAll(gv,input2)
        {
            var objForm = document.getElementById(gv);
            var objLen = objForm.rows.length;
            for (var iCount = 0; iCount < objLen; iCount++)
            {
                 objForm.rows[iCount].cells[0].getElementsByTagName("INPUT")[0].checked = input2.checked;
            }
        }
           
        function CheckSelect(gv,input2)
        {
            if (input2.checked == true)
            {
                if(GetCheckBoxCount(gv) == GetCheckedBoxCount(gv))
                {
                    form1.Checkbox3.checked = true;
                }
            }
            else
            {
                form1.Checkbox3.checked = false;
            }
        }
       
        function GetCheckBoxCount(gv)
        {
            var objForm = document.getElementById(gv);
            var objLen = objForm.rows.length;
            return (objLen-1);
        }
       
        function GetCheckedBoxCount(gv)
        {
            var count;
            count = 0;
            var objForm = document.getElementById(gv);
            var objLen = objForm.rows.length;
            for(var iCount = 0; iCount < objLen; iCount++)
            {
                 if(objForm.rows[iCount].cells[0].getElementsByTagName("INPUT")[0].checked == true)
                 {
                     count ++;
                 }
            }
            return count;
        }   
        </script>
    页面代码:
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataSourceID="LinqDataSource1" DataKeyNames="CarID">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox2" runat="server" Width="20px" onclick="CheckSelect('GridView1',this)"/>
                        </ItemTemplate>
                        <HeaderTemplate>
                            <input ID="Checkbox3" type="checkbox" style="20px" onclick="CheckAll('GridView1',this)"/>
                        </HeaderTemplate>
                        <ItemStyle Width="20px" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="CarID" HeaderText="CarID" ReadOnly="True"
                        SortExpression="CarID" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            <br />

            <asp:LinqDataSource ID="LinqDataSource1" runat="server"
                ContextTypeName="DBML.CarDataContext" Select="new (CarID, TeamID, DriverID)"
                TableName="CarInfo">
            </asp:LinqDataSource>    
        </div>
        </form>
    < /body>

    cs文件代码:
     protected void Button1_Click(object sender, EventArgs e)
            {
                int count = 0;
                string items = "";
                foreach (GridViewRow item in GridView1.Rows)
                {
                    CheckBox cb = (CheckBox)item.FindControl("CheckBox2");
                    if (cb.Checked)
                    {
                        items = items + item.Cells[1].Text;//已选行的第2列的值
                        count++;
                    }
                }
                Response.Write(count.ToString() + items);
            }

    以上代码可能通用性还有待增强,也希望对大家有所帮助。谢谢!

  • 相关阅读:
    Wwise音频解决方案概述
    图形学中的几何光学理论与视觉现象
    Visual Studio 2015 Tools for Unity使用基础
    C++编译器优化技术:RVO、NRVO和复制省略
    【ElasticSearch】 ElasticSearch基本概念和RESTful API(四)
    【ElasticStack】入门介绍(三)
    【Java】开发一个Java-SDK
    【Java】Maven 打包可运行jar包
    【SpringBoot】Spring Boot Admin 微服务应用监控
    【Java面试题】方法的参数传递机制
  • 原文地址:https://www.cnblogs.com/minotmin/p/3438722.html
Copyright © 2020-2023  润新知