您的位置:首页 > 编程语言 > ASP

ASP.NET2.0 TreeView 动态生成(两表关系)

2008-03-19 11:57 309 查看
.cs


using System;


using System.Data;


using System.Configuration;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;




public partial class _Default : System.Web.UI.Page




...{


protected void Page_Load(object sender, EventArgs e)




...{


if(!IsPostBack)


TreeViewBind();


}






private void TreeViewBind()




...{


NewsSetTableAdapters.T_WEB_NEWSCLASSTableAdapter classda = new NewsSetTableAdapters.T_WEB_NEWSCLASSTableAdapter();


NewsSetTableAdapters.T_WEB_NEWSSUBJECTTableAdapter subda = new NewsSetTableAdapters.T_WEB_NEWSSUBJECTTableAdapter();


NewsSet ds=new NewsSet();


classda.Fill(ds.T_WEB_NEWSCLASS);


subda.Fill(ds.T_WEB_NEWSSUBJECT);




foreach (DataRow masterRow in ds.T_WEB_NEWSCLASS.Rows)




...{


TreeNode masterNode = new TreeNode();


masterNode.Text = masterRow["CLASSNAME"].ToString();


masterNode.Value = masterRow["CLASSID"].ToString();




foreach (DataRow childRow in masterRow.GetChildRows("rel"))




...{


TreeNode childNode = new TreeNode();


childNode.Text = childRow["SUBJECTNAME"].ToString();


childNode.Value = childRow["SUBJECTID"].ToString();


masterNode.Expanded = false;


masterNode.ChildNodes.Add(childNode);


}


TreeView1.Nodes.Add(masterNode);


}


}


}

.aspx




<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>




<!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>


<asp:TreeView ID="TreeView1" runat="server" Height="346px" ImageSet="News" NodeIndent="10"


Width="1px">


<ParentNodeStyle Font-Bold="False" />


<HoverNodeStyle Font-Underline="True" />


<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" />


<NodeStyle Font-Names="Arial" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px"


NodeSpacing="0px" VerticalPadding="0px" />


</asp:TreeView>




</div>


</form>


</body>


</html>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: