熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET遞歸將分類綁定到TreeView

2022-06-13   來源: .NET編程 

  創建表及插入模擬數據

  CREATE TABLE [dbo][sysMenuTree](

  [NoteId] [decimal]( ) NOT NULL

  [ParentId] [decimal]( ) NULL

  [sText] [nvarchar]() NULL

  [sValue] [nvarchar]() NULL

  [sURL] [nvarchar]() NULL

  [sTarget] [nvarchar]() NULL

  [Chger] [nvarchar]() NULL

  [ChgTime] [nvarchar]() NULL

  CONSTRAINT [PK_sysMenuTree] PRIMARY KEY CLUSTERED

  (

  [NoteId] ASC

  )WITH (PAD_INDEX = OFF STATISTICS_NORECOMPUTE = OFF IGNORE_DUP_KEY = OFF ALLOW_ROW_LOCKS = ON ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

  ) ON [PRIMARY]

  insert into sysMenuTree values(N目錄N目錄)

  insert into sysMenuTree values(N目錄N目錄)

  insert into sysMenuTree values(N目錄N目錄)

  insert into sysMenuTree values(N項目N項目)

  insert into sysMenuTree values(N項目N項目)

  insert into sysMenuTree values(N項目N項目)

  insert into sysMenuTree values(N項目N項目)

  insert into sysMenuTree values(N項目N項目)

  insert into sysMenuTree values(N項目N項目)

  insert into sysMenuTree values(N項目N項目)

  在ASPNET中讀取數據綁定到TreeView實現代碼

  <%@ Page Language=C# AutoEventWireup=true  CodeFile=Defaultaspxcs Inherits=_Default %>

  <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

  <html xmlns=>

  <head runat=server>

  <title></title>

  </head>

  <body>

  <form id=form runat=server>

  <div>

  <asp:TreeView ID=treeMenu runat=server>

  </asp:TreeView>

  </div>

  </form>

  </body>

  </html>

  using System;

  using SystemWebUIWebControls;

  using SystemData;

  using SystemDataSqlClient;

  public partial class _Default : SystemWebUIPage

  {

  private readonly string ConnString = @server=\MSSQLSERVER;database=chart;uid=sa;pwd=;

  private DataTable dt = null;

  protected void Page_Load(object sender EventArgs e)

  {

  if(!IsPostBack)

  {

  dt = new DataTable();

  GetMenuToDataTable(select * from sysMenuTreedt);

  BindTree(dtnull);

  }

  }

  private void BindTree(DataTable dtSourceTreeNode parentNodestring parentID)

  {

  DataRow[] rows = dtSourceSelect(stringFormat(ParentID={}parentID));

  foreach(DataRow row in rows)

  {

  TreeNode node = new TreeNode();

  nodeText = row[sText]ToString();

  nodeValue = row[sValue]ToString();

  BindTree(dtSourcenoderow[NoteId]ToString());

  if(parentNode ==  null)

  {

  treeMenuNodesAdd(node);

  }

  else

  {

  parentNodeChildNodesAdd(node);

  }

  }

  }

  private DataTable GetMenuToDataTable(string queryDataTable dt)

  {

  using(SqlConnection conn = new SqlConnection(ConnString))

  {

  SqlCommand cmd = new SqlCommand(queryconn);

  SqlDataAdapter ada = new SqlDataAdapter(cmd);

  adaFill(dt);

  }

  return dt;

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/12278.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.