利用ASP.NET的Repeater/DataList控製項來產生高Performance的TreeView效果
這篇主要介紹如何用Repeater/DataList控製項來模擬一個高效能的TreeView結果
TreeView資料庫的結構可以參考(topcat大大的blog:ASP.NET 2.0 使用資料表動態產生TreeView的樹狀結構)來設計
c#範例
treeviewRepeater.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="treeviewRepeater.aspx.cs"
02 Inherits="treeviewRepeater" EnableViewState="false" EnableViewStateMac="false"
03 EnableSessionState="False" %>
04
05 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
06 <html>
07 <head id="Head1" runat="server">
08 <title>Untitled Page</title>
09
10 <script language="javascript">
11 function checkAll(a,b,c)
12 {
13 alert(a+" "+b+" "+c);
14 }
15 </script>
16
17 </head>
18 <body id="body" runat="server" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
19 <form id="form1" runat="server">
20 <div>
21 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="BindLocation">
22 <ItemTemplate>
23 <span style="padding-left: 0px;" />
24 <asp:CheckBox ID="CheckBox1" runat="server" /><br />
25 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="BindLocation">
26 <ItemTemplate>
27 <span style="padding-left: 20px;" />
28 <asp:CheckBox ID="CheckBox1" runat="server" /><br />
29 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="BindLocation">
30 <ItemTemplate>
31 <span style="padding-left: 40px;" />
32 <asp:CheckBox ID="CheckBox1" runat="server" /><br />
33 </ItemTemplate>
34 </asp:Repeater>
35 </ItemTemplate>
36 </asp:Repeater>
37 </ItemTemplate>
38 </asp:Repeater>
39 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
40 SelectCommand="SELECT * FROM [treeview]"></asp:SqlDataSource>
41 </div>
42 </form>
43 </body>
44 </html>
45
02 Inherits="treeviewRepeater" EnableViewState="false" EnableViewStateMac="false"
03 EnableSessionState="False" %>
04
05 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
06 <html>
07 <head id="Head1" runat="server">
08 <title>Untitled Page</title>
09
10 <script language="javascript">
11 function checkAll(a,b,c)
12 {
13 alert(a+" "+b+" "+c);
14 }
15 </script>
16
17 </head>
18 <body id="body" runat="server" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
19 <form id="form1" runat="server">
20 <div>
21 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="BindLocation">
22 <ItemTemplate>
23 <span style="padding-left: 0px;" />
24 <asp:CheckBox ID="CheckBox1" runat="server" /><br />
25 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="BindLocation">
26 <ItemTemplate>
27 <span style="padding-left: 20px;" />
28 <asp:CheckBox ID="CheckBox1" runat="server" /><br />
29 <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="BindLocation">
30 <ItemTemplate>
31 <span style="padding-left: 40px;" />
32 <asp:CheckBox ID="CheckBox1" runat="server" /><br />
33 </ItemTemplate>
34 </asp:Repeater>
35 </ItemTemplate>
36 </asp:Repeater>
37 </ItemTemplate>
38 </asp:Repeater>
39 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
40 SelectCommand="SELECT * FROM [treeview]"></asp:SqlDataSource>
41 </div>
42 </form>
43 </body>
44 </html>
45
treeviewRepeater.aspx.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class treeviewRepeater : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!IsPostBack)
17 {
18 loadDB();
19 }
20 }
21
22 protected void loadDB()
23 {
24 DataTable locations = null;
25
26 DataView childView = null;
27
28 locations = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
29
30 childView = new DataView(locations, "parentid = 0", "sText", DataViewRowState.CurrentRows);
31
32 this.Repeater1.DataSource = childView;
33
34 this.Repeater1.DataBind();
35 }
36
37 protected void BindLocation(object sender, RepeaterItemEventArgs e)
38 {
39 DataRowView locationItem = null;
40
41 DataTable locations = null;
42
43 DataView childView = null;
44
45 Repeater parentList = null;
46
47 Repeater childList = null;
48
49 CheckBox locationCheckBox = null;
50
51 if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
52 {
53
54 locationItem = (DataRowView)e.Item.DataItem;
55
56 locations = locationItem.DataView.Table;
57
58 parentList = (Repeater)sender;
59
60 locationCheckBox = (CheckBox)e.Item.Controls[1];
61
62 locationCheckBox.Text = locationItem.Row["sText"].ToString();
63
64 if (e.Item.Controls.Count > 3)
65 {
66
67 childView = new DataView(locations, "ParentId = " + locationItem.Row["NoteId"].ToString(), "sText", DataViewRowState.CurrentRows);
68
69 childList = (Repeater)e.Item.Controls[3];
70
71 childList.DataSource = childView;
72
73 childList.DataBind();
74
75 locationCheckBox.Attributes.Add("onclick", "return checkAll('" + childList.ClientID + "', '" + locationCheckBox.Parent.Parent.Parent.Controls[1].ClientID + "', " + childList.Items.Count + ");");
76
77 locationCheckBox.InputAttributes.Add("ParentId", locationCheckBox.Parent.Parent.Parent.Controls[1].ClientID);
78
79 }
80
81 else
82 {
83
84 locationCheckBox.Attributes.Add("onclick", "return checkAll('" + locationCheckBox.ClientID + "', '" + locationCheckBox.Parent.Parent.Parent.Controls[1].ClientID + "', " + parentList.Items.Count + ");");
85
86 locationCheckBox.InputAttributes.Add("ParentId", locationCheckBox.Parent.Parent.Parent.Controls[1].ClientID);
87
88 }
89
90 }
91 }
92 }
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class treeviewRepeater : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!IsPostBack)
17 {
18 loadDB();
19 }
20 }
21
22 protected void loadDB()
23 {
24 DataTable locations = null;
25
26 DataView childView = null;
27
28 locations = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
29
30 childView = new DataView(locations, "parentid = 0", "sText", DataViewRowState.CurrentRows);
31
32 this.Repeater1.DataSource = childView;
33
34 this.Repeater1.DataBind();
35 }
36
37 protected void BindLocation(object sender, RepeaterItemEventArgs e)
38 {
39 DataRowView locationItem = null;
40
41 DataTable locations = null;
42
43 DataView childView = null;
44
45 Repeater parentList = null;
46
47 Repeater childList = null;
48
49 CheckBox locationCheckBox = null;
50
51 if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
52 {
53
54 locationItem = (DataRowView)e.Item.DataItem;
55
56 locations = locationItem.DataView.Table;
57
58 parentList = (Repeater)sender;
59
60 locationCheckBox = (CheckBox)e.Item.Controls[1];
61
62 locationCheckBox.Text = locationItem.Row["sText"].ToString();
63
64 if (e.Item.Controls.Count > 3)
65 {
66
67 childView = new DataView(locations, "ParentId = " + locationItem.Row["NoteId"].ToString(), "sText", DataViewRowState.CurrentRows);
68
69 childList = (Repeater)e.Item.Controls[3];
70
71 childList.DataSource = childView;
72
73 childList.DataBind();
74
75 locationCheckBox.Attributes.Add("onclick", "return checkAll('" + childList.ClientID + "', '" + locationCheckBox.Parent.Parent.Parent.Controls[1].ClientID + "', " + childList.Items.Count + ");");
76
77 locationCheckBox.InputAttributes.Add("ParentId", locationCheckBox.Parent.Parent.Parent.Controls[1].ClientID);
78
79 }
80
81 else
82 {
83
84 locationCheckBox.Attributes.Add("onclick", "return checkAll('" + locationCheckBox.ClientID + "', '" + locationCheckBox.Parent.Parent.Parent.Controls[1].ClientID + "', " + parentList.Items.Count + ");");
85
86 locationCheckBox.InputAttributes.Add("ParentId", locationCheckBox.Parent.Parent.Parent.Controls[1].ClientID);
87
88 }
89
90 }
91 }
92 }
p.s.程式碼部分由ASP.NET討論區提供
ASP.NET 2.0 Treeview control Performance