当前位置:编程学习 > C#/ASP.NET >>

asp.net c实现省市区树形菜单代码

asp教程.net c实现省市区树形菜单代码

/// <summary>
  /// 创建dataset的方法
  /// </summary>
  /// <param name="nodes">treeview的根节点</param>
  /// <param name="ds">dataset数据集合</param>
  /// <param name="pid">节点的父</param>
  private void createtreeview(treenodecollection nodes, dataset ds, int pid)
  {
  //定义筛选条件
  string sql = string.format("parentid={0}", pid);

  //从dataset中筛选符合条件的行的集合
  datarow[] drr = ds.tables[0].select(sql);

  //遍历获得行的集合
  foreach (datarow dr in drr)
  {
  //创建treeview节点
  treenode tnode = new treenode();
  //添加到根节点中
  nodes.add(tnode);
  //设置显示的文本
  //tnode.imageurl = "images/foldericon1.gif";
  tnode.text = dr["purname"].tostring();
  tnode.value = dr["purid"].tostring();
  //设置显示的路径
  tnode.navigateurl = dr["pururl"].tostring();
  //递归调用方法
  createtreeview(tnode.childnodes, ds, convert.toint32(dr["purid"]));
  //从dataset中删除已经添加的行,提高访问效率
  ds.tables[0].rows.remove(dr);

  }
  }
 

///方法二
 

  private void treeviewbind()
    {
        string sql_tr = "select id,pid,name from tab";
        sqlconnection mysql教程conn = new sqlconnection(sqlconn);
        sqldataadapter mydataadapter = new sqldataadapter(sql_tr, mysqlconn);
        mysqlconn.open();
        mydataadapter.fill(ds_tr, "tree_show");
        dataview dv = ds_tr.tables[0].defaultview;
        dv.rowfilter = "pid=0";
        treeview1.showcheckboxes = treenodetypes.none; //不显示checkbox
        foreach (datarowview drv in dv)
        {
            treenode node = new treenode();
            node.text = drv["name"].tostring();
            node.value = drv["id"].tostring();
            node.expanded = false;
            treeview1.nodes.add(node);
            addnode(dv, node);
        }
    }

    /// 递归绑定子节点

    private void addnode(dataview dv, treenode node)
    {
        dv.rowfilter = "pid='" + node.value + "'";
        foreach (datarowview row in dv)
        {
            treenode replynode = new treenode();
            replynode.text = row["name"].tostring();
            replynode.value = row["id"].tostring();
            replynode.expanded = false;
            node.childnodes.add(replynode);
            addnode(dv, replynode);
        }
    }
 
 //方法三
 
/// treeview递归绑定数据
///先绑定province根据pif查询city
 private void inittree(treenodecollection nds,string parentid)
 {
 dataview dv=new dataview();
 treenode tmpnd;
 string intid;
 dv.table=ds.tables["tree"];
 dv.rowfilter="parentid='" + parentid + "'" ;
 foreach(datarowview drv in dv)
 {
 tmpnd=new treenode();
 tmpnd.tag=drv["nodeid"].tostring();
 tmpnd.text=drv["nodename"].tostring();
 nds.add(tmpnd);
 intid=drv["parentid"].tostring();
 inittree(tmpnd.nodes,tmpnd.tag.tostring());
 }

补充:asp.net教程,C语言
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,