ASP.NET GirdView实现折叠式效果

我们线看图的效果:

但是有个非常不爽的地方,就是GridView要进行回发操作,所以,点击或者展开折叠节点,都会进行数据的回发。

实现的原理是这样的,首先看一下绑定到GridView的datatable数据表格:

可以看到本数据集的构造方式,即parentID下面保存的都是父ID的节点,而ChildID为空;但是当某行数据为子类时,父类的 ParentID置空,而ChildID则存入子类ID值。这样,把这个数据集赋值给GridView的时候,稍微处理就可以达到开始的图片效果了,具体操作如下:

首先,需要在GridView的首列添加两个ImageButton按钮,分别为“MinBT”(-)和“PluseBT”(+),还包括一个 image标签。然后就是在RowCreated的时候,将GridView的行值赋值给imagebutton的commandargument属性,同时将datatable中数据的前两行ParentID和ChildID隐藏掉,具体代码如下:

 

  1. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  
  2.    {  
  3.        if (e.Row.RowType == DataControlRowType.Header)  
  4.        {  
  5.            e.Row.Cells[1].Visible = false;  
  6.            e.Row.Cells[2].Visible = false;  
  7.        }  
  8.        if (e.Row.RowType == DataControlRowType.DataRow)  
  9.        {  
  10.            e.Row.Cells[1].Visible = false;  
  11.            e.Row.Cells[2].Visible = false;  
  12.            ImageButton btnMin = (ImageButton)e.Row.Cells[0].FindControl("MinBT");  
  13.            btnMin.CommandArgument = e.Row.RowIndex.ToString();  
  14.            ImageButton btnAdd = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");  
  15.            btnAdd.CommandArgument = e.Row.RowIndex.ToString();  
  16.        }  
  17.    }  
 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;
            ImageButton btnMin = (ImageButton)e.Row.Cells[0].FindControl("MinBT");
            btnMin.CommandArgument = e.Row.RowIndex.ToString();
            ImageButton btnAdd = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");
            btnAdd.CommandArgument = e.Row.RowIndex.ToString();
        }
    }
 

然后,就是对按钮进行处理了,让有子节点的父类显示“+”按钮,而让没有子节点的父类显示“-”按钮,这需要在GridView1_RowDataBound事件中处理,具体代码如下:

  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.     {  
  3.         if (e.Row.RowType == DataControlRowType.DataRow)  
  4.         {  
  5.             string ShowHide = e.Row.Cells[1].Text;  
  6.             ShowHide = ShowHide.Replace(" """);      
  7.             ShowHide = ShowHide.Replace(" ","");  
  8.             if (ShowHide.Trim().Length == 0)  //如果有子类,前面就会有空白,替换掉后,长度为0,反之则不为0   
  9.             {  
  10.                 ImageButton btnMin = (ImageButton)e.Row.Cells[0].FindControl("MinBT");  
  11.                 btnMin.Visible = false;   //不显示已展开按钮   
  12.                 ImageButton btnAdd = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");  
  13.                 btnAdd.Visible = false;  //不显示可展开按钮   
  14.                 HtmlImage Line = (HtmlImage)e.Row.Cells[0].FindControl("Line");  
  15.                 Line.Visible = true;  //显示虚线框   
  16.             }  
  17.             else  
  18.             {  
  19.                 HtmlImage Line = (HtmlImage)e.Row.Cells[0].FindControl("Line");  
  20.                 Line.Visible = false;  //反之,则不现实虚线框   
  21.             }  
  22.         }  
  23.     }  
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ShowHide = e.Row.Cells[1].Text;
            ShowHide = ShowHide.Replace(" ", "");    
            ShowHide = ShowHide.Replace(" ","");
            if (ShowHide.Trim().Length == 0)  //如果有子类,前面就会有空白,替换掉后,长度为0,反之则不为0
            {
                ImageButton btnMin = (ImageButton)e.Row.Cells[0].FindControl("MinBT");
                btnMin.Visible = false;   //不显示已展开按钮
                ImageButton btnAdd = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");
                btnAdd.Visible = false;  //不显示可展开按钮
                HtmlImage Line = (HtmlImage)e.Row.Cells[0].FindControl("Line");
                Line.Visible = true;  //显示虚线框
            }
            else
            {
                HtmlImage Line = (HtmlImage)e.Row.Cells[0].FindControl("Line");
                Line.Visible = false;  //反之,则不现实虚线框
            }
        }
    }

 然后就是处理按钮的点击事件了:

 

  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  2.     {  
  3.          
  4.         if (e.CommandName == "_Show")  //当点击展开按钮时   
  5.         {  
  6.             int index = Convert.ToInt32(e.CommandArgument);  //获取第几行   
  7.             GridViewRow row = GridView1.Rows[index];  
  8.             int G_Count = GridView1.Rows.Count;  //查出总行数   
  9.             for (int i = index + 1; i < G_Count; i++)  //从本行下面一行开始   
  10.             {  
  11.                 if (GridView1.Rows[i].Cells[1].Text == " ")  //如果遇见是子类   
  12.                 {  
  13.                     GridView1.Rows[i].Visible = true;  //那么本行显示   
  14.                 }  
  15.                 else  //如果遇见的不是子类   
  16.                 {  
  17.                     ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");  
  18.                     Bt_Min.Visible = true;  //那么已展开按钮显示   
  19.                     ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");  
  20.                     Bt_plus.Visible = false;  //未展开按钮隐藏   
  21.                     break;  
  22.   
  23.                 }  
  24.                 ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");  
  25.                 Bt_Min1.Visible = true;  
  26.                 ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");  
  27.                 Bt_plus1.Visible = false;  
  28.             }  
  29.   
  30.         }  
  31.         if (e.CommandName == "_Hide")  
  32.         {  
  33.             int index = Convert.ToInt32(e.CommandArgument);  
  34.             GridViewRow row = GridView1.Rows[index];  
  35.             int G_Count = GridView1.Rows.Count;  
  36.             for (int i = index + 1; i < G_Count; i++)  
  37.             {  
  38.                 if (GridView1.Rows[i].Cells[1].Text == " ")  
  39.                 {  
  40.                     GridView1.Rows[i].Visible = false;  
  41.                 }  
  42.                 else  
  43.                 {  
  44.                     ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");  
  45.                     Bt_Min.Visible = false;  
  46.                     ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");  
  47.                     Bt_plus.Visible = true;  
  48.                     break;  
  49.   
  50.                 }  
  51.                 ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");  
  52.                 Bt_Min1.Visible = false;  
  53.                 ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");  
  54.                 Bt_plus1.Visible = true;  
  55.             }  
  56.   
  57.         }  
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        if (e.CommandName == "_Show")  //当点击展开按钮时
        {
            int index = Convert.ToInt32(e.CommandArgument);  //获取第几行
            GridViewRow row = GridView1.Rows[index];
            int G_Count = GridView1.Rows.Count;  //查出总行数
            for (int i = index + 1; i < G_Count; i++)  //从本行下面一行开始
            {
                if (GridView1.Rows[i].Cells[1].Text == " ")  //如果遇见是子类
                {
                    GridView1.Rows[i].Visible = true;  //那么本行显示
                }
                else  //如果遇见的不是子类
                {
                    ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");
                    Bt_Min.Visible = true;  //那么已展开按钮显示
                    ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");
                    Bt_plus.Visible = false;  //未展开按钮隐藏
                    break;

                }
                ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");
                Bt_Min1.Visible = true;
                ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");
                Bt_plus1.Visible = false;
            }

        }
        if (e.CommandName == "_Hide")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            int G_Count = GridView1.Rows.Count;
            for (int i = index + 1; i < G_Count; i++)
            {
                if (GridView1.Rows[i].Cells[1].Text == " ")
                {
                    GridView1.Rows[i].Visible = false;
                }
                else
                {
                    ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");
                    Bt_Min.Visible = false;
                    ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");
                    Bt_plus.Visible = true;
                    break;

                }
                ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");
                Bt_Min1.Visible = false;
                ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");
                Bt_plus1.Visible = true;
            }

        }

 

 所有代码如下:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Web.UI.HtmlControls;  
  10.   
  11. public partial class Default2 : System.Web.UI.Page  
  12. {  
  13.     public string connStr = System.Configuration.ConfigurationManager.AppSettings["connStr"];  
  14.   
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         if (!IsPostBack)  
  18.         {  
  19.             using (SqlConnection conn = new SqlConnection(connStr))  
  20.             {  
  21.                 conn.Open();  
  22.                 string sql = "SELECT ParentName,ParentID,dateof FROM TreeParent tp ";  
  23.                 SqlCommand cmd = new SqlCommand(sql, conn);  
  24.                 DataTable dt = new DataTable();  
  25.                 dt.Columns.Add(new DataColumn("ParentID"typeof(string)));  
  26.                 dt.Columns.Add(new DataColumn("ChildID"typeof(string)));  
  27.                 dt.Columns.Add(new DataColumn("Name"typeof(string)));  
  28.                 dt.Columns.Add(new DataColumn("Dateof"typeof(string)));  
  29.                 SqlDataReader sdr = cmd.ExecuteReader();  
  30.                 while (sdr.Read())  
  31.                 {  
  32.                     DataRow dr = dt.NewRow();  
  33.                     int pID = Convert.ToInt32(sdr["ParentID"]);  
  34.                     dr["ParentID"] = (sdr["ParentID"]);  
  35.                     dr["ChildID"] = null;  
  36.                     dr["Name"] = sdr["ParentName"];  
  37.                     dr["Dateof"] = sdr["Dateof"];  
  38.                     dt.Rows.Add(dr);  
  39.                     DataTable myDT = GenerateDT(pID.ToString());  
  40.                     foreach (DataRow drChild in myDT.Rows)  
  41.                     {  
  42.                         DataRow drChildDT = dt.NewRow();  
  43.                         drChildDT["ChildID"] = drChild["ChildID"];  
  44.                         drChildDT["Name"] = drChild["ChildName"];  
  45.                         drChildDT["Dateof"] = drChild["Dateof"];  
  46.                         dt.Rows.Add(drChildDT);  
  47.                     }  
  48.                 }  
  49.                 sdr.Close();  
  50.                 GridView1.DataSource = dt;  
  51.                 GridView1.DataBind();  
  52.             }  
  53.   
  54.         }  
  55.     }  
  56.   
  57.     protected DataTable GenerateDT(string ParentID)  
  58.     {  
  59.         using (SqlConnection conn = new SqlConnection(connStr))  
  60.         {  
  61.             conn.Open();  
  62.   
  63.             DataTable dt = new DataTable();  
  64.             dt.Columns.Add(new DataColumn("ChildName"typeof(string)));  
  65.             dt.Columns.Add(new DataColumn("ChildID"typeof(string)));  
  66.             dt.Columns.Add(new DataColumn("Dateof"typeof(string)));  
  67.             string sql = "SELECT childID,childName,dateof FROM TreeChild tc where tc.ParentID=" + ParentID;  
  68.             SqlCommand cmd = new SqlCommand(sql, conn);  
  69.             SqlDataReader sdr = cmd.ExecuteReader();  
  70.             while (sdr.Read())  
  71.             {  
  72.                 DataRow dr = dt.NewRow();  
  73.                 dr["ChildName"] = sdr["childName"];  
  74.                 dr["ChildID"] = sdr["ChildID"];  
  75.                 dr["Dateof"] = sdr["Dateof"];  
  76.                 dt.Rows.Add(dr);  
  77.             }  
  78.             sdr.Close();  
  79.             return dt;  
  80.         }  
  81.     }  
  82.   
  83.   
  84.     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  
  85.     {  
  86.         if (e.Row.RowType == DataControlRowType.Header)  
  87.         {  
  88.             e.Row.Cells[1].Visible = false;  
  89.             e.Row.Cells[2].Visible = false;  
  90.         }  
  91.         if (e.Row.RowType == DataControlRowType.DataRow)  
  92.         {  
  93.             e.Row.Cells[1].Visible = false;  
  94.             e.Row.Cells[2].Visible = false;  
  95.             ImageButton btnMin = (ImageButton)e.Row.Cells[0].FindControl("MinBT");  
  96.             btnMin.CommandArgument = e.Row.RowIndex.ToString();  
  97.             ImageButton btnAdd = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");  
  98.             btnAdd.CommandArgument = e.Row.RowIndex.ToString();  
  99.         }  
  100.     }  
  101.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  102.     {  
  103.         if (e.Row.RowType == DataControlRowType.DataRow)  
  104.         {  
  105.             string ShowHide = e.Row.Cells[1].Text;  
  106.             ShowHide = ShowHide.Replace(" """);  
  107.             ShowHide = ShowHide.Replace(" ","");  
  108.             if (ShowHide.Trim().Length == 0)  
  109.             {  
  110.                 ImageButton btnMin = (ImageButton)e.Row.Cells[0].FindControl("MinBT");  
  111.                 btnMin.Visible = false;  
  112.                 ImageButton btnAdd = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");  
  113.                 btnAdd.Visible = false;  
  114.                 HtmlImage Line = (HtmlImage)e.Row.Cells[0].FindControl("Line");  
  115.                 Line.Visible = true;  
  116.             }  
  117.             else  
  118.             {  
  119.                 HtmlImage Line = (HtmlImage)e.Row.Cells[0].FindControl("Line");  
  120.                 Line.Visible = false;  
  121.             }  
  122.         }  
  123.     }  
  124.     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  125.     {  
  126.          
  127.         if (e.CommandName == "_Show")  
  128.         {  
  129.             int index = Convert.ToInt32(e.CommandArgument);  
  130.             GridViewRow row = GridView1.Rows[index];  
  131.             int G_Count = GridView1.Rows.Count;  
  132.             for (int i = index + 1; i < G_Count; i++)  
  133.             {  
  134.                 if (GridView1.Rows[i].Cells[1].Text == " ")  
  135.                 {  
  136.                     GridView1.Rows[i].Visible = true;  
  137.                 }  
  138.                 else  
  139.                 {  
  140.                     ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");  
  141.                     Bt_Min.Visible = true;  
  142.                     ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");  
  143.                     Bt_plus.Visible = false;  
  144.                     break;  
  145.   
  146.                 }  
  147.                 ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");  
  148.                 Bt_Min1.Visible = true;  
  149.                 ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");  
  150.                 Bt_plus1.Visible = false;  
  151.             }  
  152.   
  153.         }  
  154.         if (e.CommandName == "_Hide")  
  155.         {  
  156.             int index = Convert.ToInt32(e.CommandArgument);  
  157.             GridViewRow row = GridView1.Rows[index];  
  158.             int G_Count = GridView1.Rows.Count;  
  159.             for (int i = index + 1; i < G_Count; i++)  
  160.             {  
  161.                 if (GridView1.Rows[i].Cells[1].Text == " ")  
  162.                 {  
  163.                     GridView1.Rows[i].Visible = false;  
  164.                 }  
  165.                 else  
  166.                 {  
  167.                     ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");  
  168.                     Bt_Min.Visible = false;  
  169.                     ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");  
  170.                     Bt_plus.Visible = true;  
  171.                     break;  
  172.   
  173.                 }  
  174.                 ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");  
  175.                 Bt_Min1.Visible = false;  
  176.                 ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");  
  177.                 Bt_plus1.Visible = true;  
  178.             }  
  179.   
  180.         }  
  181.           
  182.     }  
  183. }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
    public string connStr = System.Configuration.ConfigurationManager.AppSettings["connStr"];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                string sql = "SELECT ParentName,ParentID,dateof FROM TreeParent tp ";
                SqlCommand cmd = new SqlCommand(sql, conn);
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("ParentID", typeof(string)));
                dt.Columns.Add(new DataColumn("ChildID", typeof(string)));
                dt.Columns.Add(new DataColumn("Name", typeof(string)));
                dt.Columns.Add(new DataColumn("Dateof", typeof(string)));
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    DataRow dr = dt.NewRow();
                    int pID = Convert.ToInt32(sdr["ParentID"]);
                    dr["ParentID"] = (sdr["ParentID"]);
                    dr["ChildID"] = null;
                    dr["Name"] = sdr["ParentName"];
                    dr["Dateof"] = sdr["Dateof"];
                    dt.Rows.Add(dr);
                    DataTable myDT = GenerateDT(pID.ToString());
                    foreach (DataRow drChild in myDT.Rows)
                    {
                        DataRow drChildDT = dt.NewRow();
                        drChildDT["ChildID"] = drChild["ChildID"];
                        drChildDT["Name"] = drChild["ChildName"];
                        drChildDT["Dateof"] = drChild["Dateof"];
                        dt.Rows.Add(drChildDT);
                    }
                }
                sdr.Close();
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

        }
    }

    protected DataTable GenerateDT(string ParentID)
    {
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ChildName", typeof(string)));
            dt.Columns.Add(new DataColumn("ChildID", typeof(string)));
            dt.Columns.Add(new DataColumn("Dateof", typeof(string)));
            string sql = "SELECT childID,childName,dateof FROM TreeChild tc where tc.ParentID=" + ParentID;
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                DataRow dr = dt.NewRow();
                dr["ChildName"] = sdr["childName"];
                dr["ChildID"] = sdr["ChildID"];
                dr["Dateof"] = sdr["Dateof"];
                dt.Rows.Add(dr);
            }
            sdr.Close();
            return dt;
        }
    }


    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;
            ImageButton btnMin = (ImageButton)e.Row.Cells[0].FindControl("MinBT");
            btnMin.CommandArgument = e.Row.RowIndex.ToString();
            ImageButton btnAdd = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");
            btnAdd.CommandArgument = e.Row.RowIndex.ToString();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ShowHide = e.Row.Cells[1].Text;
            ShowHide = ShowHide.Replace(" ", "");
            ShowHide = ShowHide.Replace(" ","");
            if (ShowHide.Trim().Length == 0)
            {
                ImageButton btnMin = (ImageButton)e.Row.Cells[0].FindControl("MinBT");
                btnMin.Visible = false;
                ImageButton btnAdd = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");
                btnAdd.Visible = false;
                HtmlImage Line = (HtmlImage)e.Row.Cells[0].FindControl("Line");
                Line.Visible = true;
            }
            else
            {
                HtmlImage Line = (HtmlImage)e.Row.Cells[0].FindControl("Line");
                Line.Visible = false;
            }
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        if (e.CommandName == "_Show")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            int G_Count = GridView1.Rows.Count;
            for (int i = index + 1; i < G_Count; i++)
            {
                if (GridView1.Rows[i].Cells[1].Text == " ")
                {
                    GridView1.Rows[i].Visible = true;
                }
                else
                {
                    ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");
                    Bt_Min.Visible = true;
                    ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");
                    Bt_plus.Visible = false;
                    break;

                }
                ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");
                Bt_Min1.Visible = true;
                ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");
                Bt_plus1.Visible = false;
            }

        }
        if (e.CommandName == "_Hide")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            int G_Count = GridView1.Rows.Count;
            for (int i = index + 1; i < G_Count; i++)
            {
                if (GridView1.Rows[i].Cells[1].Text == " ")
                {
                    GridView1.Rows[i].Visible = false;
                }
                else
                {
                    ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");
                    Bt_Min.Visible = false;
                    ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");
                    Bt_plus.Visible = true;
                    break;

                }
                ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");
                Bt_Min1.Visible = false;
                ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");
                Bt_plus1.Visible = true;
            }

        }
        
    }
}

前台代码如下:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.       
  13.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"   
  14.             CellPadding="4" ForeColor="#333333" GridLines="None"   
  15.             onrowcommand="GridView1_RowCommand" onrowcreated="GridView1_RowCreated"   
  16.             onrowdatabound="GridView1_RowDataBound" Width="456px">  
  17.             <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  18.             <Columns>  
  19.                 <asp:TemplateField HeaderText="操作">  
  20.                     <ItemTemplate>  
  21.                         <asp:ImageButton ID="PluseBT" runat="server" Visible="false" CommandName="_Show" BackColor="White" BorderStyle="none" ImageUrl="~/plus.gif" />  
  22.                         <asp:ImageButton ID="MinBT" runat="server" CommandName="_Hide" BackColor="White" BorderStyle="none" ImageUrl="~/minus.gif" />  
  23.                         <img id="Line" runat="server" visible="false" src="~/line.gif"/>  
  24.                     </ItemTemplate>  
  25.                 </asp:TemplateField>  
  26.                 <asp:BoundField DataField="ParentID" HeaderText="Parent Id" />  
  27.                 <asp:BoundField DataField="ChildId" HeaderText="Child Id" />  
  28.                 <asp:BoundField DataField="Name" HeaderText="名称" />  
  29.                 <asp:BoundField DataField="Dateof" HeaderText="时间" />  
  30.             </Columns>  
  31.   
  32.             <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  33.             <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  34.             <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  35.             <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  36.             <EditRowStyle BackColor="#999999" />  
  37.             <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  38.         </asp:GridView>  
  39.       
  40.     </div>  
  41.     </form>  
  42. </body>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值