目的:
通过TreeView控件的运用,从Sql2005的表中生成两级联动的树状菜单。
最终的显示效果:
数据库test,主类表为big,子类表为class
aspx中的TreeView控件的代码
cs中的代码:
通过TreeView控件的运用,从Sql2005的表中生成两级联动的树状菜单。
最终的显示效果:
数据库test,主类表为big,子类表为class
aspx中的TreeView控件的代码
Code
<asp:TreeView ID="TreeView1" runat="server" Height="168px" ImageSet="Simple" Width="175px">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD"
HorizontalPadding="0px" VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black"
HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
<asp:TreeView ID="TreeView1" runat="server" Height="168px" ImageSet="Simple" Width="175px">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD"
HorizontalPadding="0px" VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black"
HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
cs中的代码:
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//填充一级菜单节点
DataView dv = drv("select * from big");
foreach (DataRowView datav in dv)
{
TreeNode tn = new TreeNode();
tn.Text = datav["title"].ToString();
tn.Value = datav["bid"].ToString();
//一级菜单是折叠状态
tn.Expanded = false;
TreeView1.Nodes.Add(tn);
AddChildNodes(tn);
}
}
}
/**//// <summary>
/// 根据所对应的一级菜单ID,来填充二级菜单节点
/// </summary>
/// <param name="tn"></param>
private void AddChildNodes(TreeNode tn)
{
//先获取一级菜单的ID
int bid = int.Parse(tn.Value);
DataView dv = drv("select * from class where bid=" + bid);
foreach (DataRowView datav in dv)
{
TreeNode ctn = new TreeNode();
ctn.Text = datav["title"].ToString();
ctn.Value = datav["cid"].ToString();
ctn.NavigateUrl = "TreeView.aspx?cid=" + datav["cid"].ToString();
tn.ChildNodes.Add(ctn);
}
}
/**//// <summary>
/// 根据Sql语句返回数据视图
/// </summary>
/// <param name="sql">要执行的sql语句</param>
/// <returns></returns>
private DataView drv(string sql)
{
string connString = "server=.; uid=sa; pwd=qaz!123; database=test";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds,"tree");
return ds.Tables["tree"].DefaultView;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//填充一级菜单节点
DataView dv = drv("select * from big");
foreach (DataRowView datav in dv)
{
TreeNode tn = new TreeNode();
tn.Text = datav["title"].ToString();
tn.Value = datav["bid"].ToString();
//一级菜单是折叠状态
tn.Expanded = false;
TreeView1.Nodes.Add(tn);
AddChildNodes(tn);
}
}
}
/**//// <summary>
/// 根据所对应的一级菜单ID,来填充二级菜单节点
/// </summary>
/// <param name="tn"></param>
private void AddChildNodes(TreeNode tn)
{
//先获取一级菜单的ID
int bid = int.Parse(tn.Value);
DataView dv = drv("select * from class where bid=" + bid);
foreach (DataRowView datav in dv)
{
TreeNode ctn = new TreeNode();
ctn.Text = datav["title"].ToString();
ctn.Value = datav["cid"].ToString();
ctn.NavigateUrl = "TreeView.aspx?cid=" + datav["cid"].ToString();
tn.ChildNodes.Add(ctn);
}
}
/**//// <summary>
/// 根据Sql语句返回数据视图
/// </summary>
/// <param name="sql">要执行的sql语句</param>
/// <returns></returns>
private DataView drv(string sql)
{
string connString = "server=.; uid=sa; pwd=qaz!123; database=test";
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds,"tree");
return ds.Tables["tree"].DefaultView;
}
cs中引用两个命名空间:
using System.Data;
using System.Data.SqlClient;
OK,大功告成,两级树状菜单完成。且主菜单负责展开和合拢,而二级子菜单为导航链接。
using System.Data;
using System.Data.SqlClient;
OK,大功告成,两级树状菜单完成。且主菜单负责展开和合拢,而二级子菜单为导航链接。