ASP.NET上怎么运用TreeView

分类: asp.net   出处:iocblog整理  更新时间:2009-05-08   添加到收藏  


  首先需要创建一个数据库表test,字段包括nodeid、parentid、nodename、adderss、icon、linkurlnodeid是在数据库中作为节点的唯一标识,这里为了方便,将其设置为标识;
  
  parentid表示每个节点的上级节点id,如该节点无上级,则为0;
  
  nodename是节点在页面上显示的名称;
  
  以上三个字段应不能为空
  
  adderss地址;icon节点图片;linkurl节点链接地址;
  
  以上三个字段用与不用不是必须的,因实际情况而定,这里因为使用到所以列出来
  
  接着向设计窗体添加一个treeview控件如果还没添加过此控件的请到以下地址下载并安装,添加引用microsoft.web.ui.webcontrols.dll然后到工具箱内添加控件http://msdn.microsoft.com/archive/en-us/samples/internet/asp_dot_net_servercontrols/webcontrols/default.asp
  
  下面是aspx.cs部分的代码
  
  using system;
  using system.collections;
  using system.componentmodel;using system.data;using system.drawing;
  using system.web;using system.web.sessionstate;
  using system.web.ui;using system.web.ui.webcontrols;
  using system.web.ui.htmlcontrols;
  using microsoft.web.ui.webcontrols;
  namespace ioa{
  /// <summary> /// webform1 的摘要说明。
  /// </summary>
  public class webform1 : system.web.ui.page
  { protected microsoft.web.ui.webcontrols.treeview treeview1;(www.iocblog.net 文章来源)
  protected classes.deptartment mydt=new ioa.classes.deptartment();//我把与数据库链接以及对数据库的操作请求写在此类中    这里数据库的链接就不具体写出来了,链接数据库以后    通过select * from test返回值传给程序中的ds变    量即可,记得返回的值的类型是dataset
  private void page_load(object sender, system.eventargs e)
  {  inittreeview(this.treeview1.nodes);
  // 在此处放置用户代码以初始化页面 }
  public void inittreeview(treenodecollection node)
  {
  this.inittree(node,"0");
  }
  public void inittree(treenodecollection nds,string parentid)
  {
  dataset ds=new dataset();
  ds=mydt.treeinfo();
  dataview dv = new dataview();
  treenode tmpnd;
  string intid;
  dv.table = ds.tables[0];
  dv.rowfilter = "parentid = " + parentid;
  foreach(datarowview drv in dv)  {
  tmpnd = new treenode();
  tmpnd.id = drv["nodeid"].tostring();
  if(drv["linkurl"].tostring().trim() != "")
  {
  tmpnd.text = "<a href ='"+drv["linkurl"].tostring().trim() +"'target='mainframe'>"+drv["nodename"].tostring()+"</a>";
  }
  else
  {
  tmpnd.text = drv["nodename"].tostring();
  }
  nds.add(tmpnd);
  intid = drv["parentid"].tostring();
  inittree(tmpnd.nodes,tmpnd.id);
  }
  }
  #region web 窗体设计器生成的代码 override protected void oninit(eventargs e)
  {
  //
  // codegen: 该调用是 asp.net web 窗体设计器所必需的。
  //
  initializecomponent();
  base.oninit(e);
  }
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary> private void initializecomponent()
  {
  this.load += new system.eventhandler(this.page_load);
  } #endregion }}(www.iocblog.net 文章来源)


Tag: TreeView



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。