GridView三层嵌套加折叠

来源:互联网 发布:如何优化剑灵 编辑:程序博客网 时间:2024/05/14 02:24
   今天在开发遇到一个问题,需求三层嵌套,于是根据孟子的两层嵌套加折叠的基础上,增加为三层嵌套!
下面为效果图:

 1protected void Page_Load(object sender, EventArgs e)
 2    {
 3        if (!IsPostBack)
 4        {
 5            BindParent();
 6        }

 7    }

 8
 9    private void BindParent()
10    {
11        string sql = "select * from PageLink where PID=0 order by ID desc";
12        DataSet ds = Iwoak.ExecSQL.ExecuteDataset(sql);
13        gvParent.DataSource = ds.Tables[0].DefaultView;
14        gvParent.DataBind();
15    }

16  
17
18    protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
19    {
20        if (e.Row.RowType == DataControlRowType.DataRow)
21        {
22            GridView gvChild = (GridView)e.Row.FindControl("gvChild");
23            Label lblID = (Label)e.Row.FindControl("lblID");
24            string sql = "select * from PageLink where PID=" + lblID.Text;
25            DataSet ds = Iwoak.ExecSQL.ExecuteDataset(sql);
26            gvChild.DataSource = ds.Tables[0].DefaultView;
27            gvChild.RowDataBound += new GridViewRowEventHandler(gvChild_RowDataBound);
28            gvChild.DataBind();
29        }

30    }

31
32
33    protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
34    {
35        if (e.Row.RowType == DataControlRowType.DataRow)
36        {
37            GridView gvlink = (GridView)e.Row.FindControl("gvlink");
38            Label lblID = (Label)e.Row.FindControl("lbl_ID");
39            string sql = "select * from PageLink where PID=" + lblID.Text;
40            DataSet ds = Iwoak.ExecSQL.ExecuteDataset(sql);
41            gvlink.DataSource = ds.Tables[0].DefaultView;
42            gvlink.DataBind();
43        }

44    }

 1<head runat="server">
 2    <title>内嵌页链接--列表页面</title>
 3    <script type="text/javascript" language="javascript" >
 4 function ShowHidden(sid,ev)
 5  {
 6    ev = ev || window.event;
 7    var target = ev.target || ev.srcElement;
 8    var oDiv = document.getElementById("div" + sid);
 9    oDiv.style.display = oDiv.style.display == "none"?"block":"none";
10    target.title = oDiv.style.display == "none"?"显示":"隐藏";
11    var imgid='img'+sid;   
12    document.getElementById(imgid).src=oDiv.style.display == "none"?"../CommonFiles/images/open.gif":"../CommonFiles/images/close1.gif";
13  }

14
</script>
15</head>
16<body>
17    <form id="form1" runat="server">
18    
19        <asp:GridView ID="gvParent" runat="server" Width="100%"  AutoGenerateColumns="False" 
20        EmptyDataText='<font color="red" class="GridViewItem" >没有找到相关数据!</font>
21          OnRowDataBound="gvParent_RowDataBound" >
22            <EmptyDataRowStyle HorizontalAlign="Center" />
23            <HeaderStyle ForeColor="White" HorizontalAlign="Center" CssClass="GridViewHeader" />
24            <PagerSettings Visible="False" />
25            <Columns>
26                <asp:TemplateField HeaderText="名称">
27                 <itemtemplate>
28                    <id='<%#Eval("ID") %>' onclick="ShowHidden('<%#Eval("id"%>',event)" href="#">
29                    <IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" id='img<%# Eval("ID")%>
30                    alt="展开/折叠" src="../CommonFiles/images/close1.gif" /> <%Eval("name")%></A>
31                    <asp:Label ID="lblID" runat="server" Text='<%#Eval("id") %>'  style="display:none;"></asp:Label>
32                     <div id="div<%# Eval("ID")%>" >
33                     <asp:GridView id="gvChild" runat="server"  OnRowDataBound="gvChild_RowDataBound" CssClass="inputAwoke" Width="95%"  AutoGenerateColumns="False" ShowHeader="False" BorderWidth="0px" HorizontalAlign="Center">
34                         <Columns>
35                             <asp:TemplateField>
36                              <itemtemplate>
37                               <id='<%#Eval("ID") %>' onclick="ShowHidden('<%#Eval("id"%>',event)" href="#">
38                                    <IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" id='img<%# Eval("ID")%>
39                                     alt="展开/折叠" src="../CommonFiles/images/close1.gif" /> <%Eval("name")%></A>
40                             <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("id") %>'  style="display:none;"></asp:Label>
41                                 <div id="div<%# Eval("ID")%>" >
42                                <asp:GridView id="gvlink" runat="server" CssClass="inputAwoke" Width="92%"  AutoGenerateColumns="False"
43                                 ShowHeader="False" BorderWidth="0px" HorizontalAlign="Center">
44                                 <EmptyDataRowStyle HorizontalAlign="Center" />
45                                  <HeaderStyle ForeColor="White" HorizontalAlign="Center" CssClass="GridViewHeader" />
46                                 <PagerSettings Visible="False" />
47                                    <Columns>
48                                             <asp:TemplateField>
49                                             <itemtemplate>
50                                           <href="<%# Eval("UrlPath")%>" target="_blank"><%Eval("name")%></a>  
51                                             </itemtemplate>
52                                             <ItemStyle CssClass="GridViewItem" />
53                                             </asp:TemplateField>
54                                    </Columns>
55                                </asp:GridView>
56                                             
57                                 </div>
58                              </itemtemplate>
59                               <ItemStyle CssClass="GridViewItem" />
60                             </asp:TemplateField>
61                         </Columns>
62                    
63                    </asp:GridView>
64                     </div>
65                </itemtemplate>
66                    <ItemStyle CssClass="GridViewItem" />
67                </asp:TemplateField>
68               
69            </Columns>
70        </asp:GridView>
71    
72    </form>
73</body>
 
原创粉丝点击