ChennelList.aspx (無限級列表)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChennelList.aspx.cs" Inherits="YE_admin_ChennelList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="Stylesheet" href="css/css.css" />
</head>
<body>
<form id="form1" runat="server">
<div >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderColor="#3399FF" CellPadding="3" ShowFooter="True" onprerender="GridView1_PreRender"
Width="80%" HorizontalAlign="Center">
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<Columns>
<asp:BoundField DataField="id" HeaderText="編號" >
<ItemStyle Width="10%" HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ch_name" HeaderText="欄目" >
<ItemStyle Width="60%" />
</asp:BoundField>
<asp:BoundField DataField="ch_order" HeaderText="排序" >
<ItemStyle Width="15%" HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField><ItemTemplate>
<asp:HyperLink ID="edit" runat="server">修改</asp:HyperLink>
<asp:HyperLink ID="del" runat="server">刪除</asp:HyperLink>
</ItemTemplate>
<ItemStyle Width="15%" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
</div>
</form>
</body>
</html>
ChennelList.aspx.cs
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.OleDb;
public partial class YE_admin_ChennelList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
DataTable table = new DataTable();
protected void GridView1_PreRender(object sender, EventArgs e)
{
CommFun.IsAdmin();
string sql = "select * from Ye_chennel ";
DataTable dt = DBFun.GetDataTable(sql);
DataColumn dc = new DataColumn("id", typeof(Int64));
DataColumn dc1 = new DataColumn("ch_name", typeof(string));
DataColumn dc2 = new DataColumn("ch_order", typeof(Int64));
table.Columns.Add(dc);
table.Columns.Add(dc1);
table.Columns.Add(dc2);
foreach (DataRow dr in dt.Rows)
{
if (Convert.ToInt64(dr["ch_father"]) == 0)
{
DataRow temprow = table.NewRow();
temprow["id"] = dr["id"];
temprow["ch_name"] = dr["ch_name"];
temprow["ch_order"] = dr["ch_order"];
table.Rows.Add(temprow);
ChildItem(dt, Convert.ToInt64(dr["id"]), 1);
}
}
GridView1.DataSource = table.DefaultView;
GridView1.DataBind();
foreach (GridViewRow gr in GridView1.Rows)
{
HyperLink edit = (HyperLink)gr.FindControl("edit");
HyperLink del = (HyperLink)gr.FindControl("del");
string id = gr.Cells[0].Text;
edit.NavigateUrl = "ChennelEdit.aspx?id=" + id + "";
del.NavigateUrl = "ChennelDel.aspx?id=" + id + "";
}
}
/// <summary>
/// 獲得子節點
/// </summary>
/// <param name="dt"></param>
/// <param name="id">父ID</param>
/// <param name="length">子節點前面的空格數</param>
public void ChildItem(DataTable dt,long id,int length)
{
DataRow[] rows = dt.Select("ch_father='" + id + "'"); ////取出id子節點進行綁定
for (int i = 0; i < rows.Length; i++)
{
DataRow row = table.NewRow();
row["id"] = rows[i]["id"];
row["ch_name"] = Space(length) + "├" + rows[i]["ch_name"];
row["ch_order"] = rows[i]["ch_order"];
table.Rows.Add(row);
ChildItem(dt, Convert.ToInt64(rows[i]["id"]), length + 1);
}
}
public string Space(int length)
{
string temp="";
for (int i = 0; i < length; i++)
{
temp += " "; ////注意這裏的空白是智能abc輸入法狀態下的v11字符;
}
return temp;
}
}
ChennelDel.aspx (刪除)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChennelDel.aspx.cs" Inherits="YE_admin_ChennelDel" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
ChennelDel.aspx .cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class YE_admin_ChennelDel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id = Convert.ToInt32(Request.QueryString["id"]);
string sql="select * from Ye_chennel where id="+id+"";
DataRow dr = DBFun.GetDataRow(sql);
string oldChilds = dr["ch_childs"].ToString();
string oldChild = id.ToString();
int father=Convert.ToInt32(dr["ch_father"]);
//刪除其childs下的節點
delChilds(oldChilds);
//其父節點中刪除childs
delFatherChilds(oldChilds, father);
//刪除父節點中的child
delFatherChild(oldChild, father);
Response.Redirect(Request.UrlReferrer.ToString());
}
}
/// <summary>
/// //刪除其childs下的節點
/// </summary>
/// <param name="oldChilds">待刪除行的childs</param>
public void delChilds(string oldChilds)
{
char[] duohao = { ',' };
string[] arrStr = oldChilds.Split(duohao);
foreach (string m in arrStr)
{
if (m != "0")
{
DBFun.ExecuteSql("delete from Ye_chennel where id=" + Convert.ToInt32(m) + "");
}
}
}
/// <summary>
/// 其父節點中刪除childs
/// </summary>
/// <param name="oldChilds">待刪除行的childs</param>
/// <param name="father">父ID</param>
public void delFatherChilds(string oldChilds, int father)
{
if(father!=0)
{
char[] duohao = { ',' };
string[] arrStr = oldChilds.Split(duohao);
DataRow fatherRow = DBFun.GetDataRow("select * from Ye_chennel where id=" + father + ""); //父行
string fatherchilds = fatherRow["ch_childs"].ToString();
int ff=Convert.ToInt32(fatherRow["ch_father"]);
foreach (string m in arrStr)
{
if (fatherchilds.IndexOf(m)>=0)
{
fatherchilds = fatherchilds.Replace("," + m, "");
}
}
DBFun.ExecuteSql("update Ye_chennel set ch_childs='" + fatherchilds + "' where id=" + father + "");
delFatherChilds(oldChilds,ff);
}
}
/// <summary>
/// 其父節點中刪除child
/// </summary>
/// <param name="oldChild">待刪除行的child</param>
/// <param name="father">父ID</param>
public void delFatherChild(string oldChild, int father)
{
if (father != 0)
{
DataRow fatherRow = DBFun.GetDataRow("select * from Ye_chennel where id=" + father + ""); //父行
string fatherchild = fatherRow["ch_child"].ToString();
if (fatherchild.IndexOf(",") >= 0)
{
if (fatherchild.IndexOf("," + oldChild) >= 0)
{
fatherchild = fatherchild.Replace("," + oldChild, "");
}
else
{
if (fatherchild.IndexOf(oldChild + ",") >= 0)
{
fatherchild = fatherchild.Replace(oldChild + ",", "");
}
}
}
else
{
fatherchild = "0";
}
DBFun.ExecuteSql("update Ye_chennel set ch_child='" + fatherchild + "' where id=" + father + "");
}
}
}