zoukankan      html  css  js  c++  java
  • Gridview 实现列表全选、自动选择下级item的功能

    Gridview 实现列表全选、自动选择下级item的功能

    1、  前台关键代码

    <asp:TemplateField HeaderText="选择" ItemStyle-HorizontalAlign="Center">

    <ItemTemplate>

    <asp:CheckBox ID="chkitem" runat="server" Enabled="true" Style="cursor: pointer" AutoPostBack="true" OnCheckedChanged="chkitem_selectchange" />

    </ItemTemplate>

    <HeaderTemplate>

    <asp:CheckBox ID="chkall" runat="server" Text="选择" OnCheckedChanged="chkall_selectchange" AutoPostBack="true"/>

    </HeaderTemplate>

    </asp:TemplateField>

    注意:

    AutoPostBack="true" OnCheckedChanged="chkitem_selectchange"

    OnCheckedChanged="chkall_selectchange" AutoPostBack="true"

    这两行代码分别是实现全选和ITEM单选按钮,如图

    实现功能如下:

    1、 在列表头点选择实现列表项全选或全不选功能

    2、 单独选择一个列表时自动根看成机构ID”实现选择或取消下级机构的选择功能。

     

    后台代码:

    Page.cs页面增加“chkitem_selectchange chkall_selectchange两个函数

    /// <summary>

    /// 全选操作

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void chkall_selectchange(object sender, EventArgs e)

    {

            isCheckall = true;

     

            foreach (GridViewRow gvr in this.gv.Rows)

            {

                CheckBox cb = (CheckBox)gvr.Cells[0].FindControl("chkitem");

                cb.Checked = ((CheckBox)sender).Checked;

            }

     

            //标志置为初始状态,防止项目里的selectchange事件重复执行

            isCheckall = false;

    }

    /// <summary>

    /// 实现自动勾选下级机构功能

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void chkitem_selectchange(object sender, EventArgs e)

    {

            if (!isCheckall)

            {

                CheckBox cb = (CheckBox)sender;

                GridViewRow gvr = (GridViewRow)cb.Parent.Parent;

                string v_code = gvr.Cells[1].Text;

                foreach (GridViewRow row in this.gv.Rows)

                {

                    string v_tmp = row.Cells[1].Text;

                    if (v_tmp.Length >= v_code.Length && v_tmp.Substring(0, v_code.Length).Equals(v_code))

                    {

                        CheckBox rcb = (CheckBox)row.Cells[0].FindControl("chkitem");

                        rcb.Checked = cb.Checked;

                    }

                }

            }

    }

  • 相关阅读:
    变量的作用域
    内联函数inline
    数组、函数和指针
    关于android:configChanges的属性的简介
    Android 更新UI的两种方法
    android开发两种退出程序方式
    google内购In-App Billing
    谷歌登陆sdk对接
    openssl测试版本小工具
    关于facebook登陆不安装openssl的情况下怎么获得Facebook Key Hash的简单方法
  • 原文地址:https://www.cnblogs.com/hzj3099/p/3357305.html
Copyright © 2011-2022 走看看