color=#FF0000]  请看清楚问题在回答,请不要敷衍。[/color]
    问题如下:目前存在三个TreeView,名称分别为:TreeView1,TreeView2,TreeView3。
              我想根据字符串判断哪个TreeView显示。
              字符串:string str="1,2";
              这样就只有TreeView1和TreeView2显示出来。
                    怎么求解???
目前可以这样写:
       string str = "1,2";
       string[] arr = str.Split(',');
       int num = arr.Length;
       for (int i = 0; i < num; i++)
       {
           int aa =int.Parse(str.Split(',')[i]);
           Response.Write(aa+"  "); // 1 2
           (TreeView("TreeView"+i)) //不能用
       }
怎么办??

解决方案 »

  1.   


      string str = "1,2";
      string[] arr = str.Split(',');
      int num = arr.Length;
      for (int i = 0; i < num; i++)
      {
        int aa =int.Parse(str.Split(',')[i]);
        Response.Write(aa+" "); // 1 2
        TreeView tv = this.Page.FindControl("TreeView"+i);--改为这样
        //接下来对这个tv进行操作
      }
      

  2.   

    string str="1,2";
    TreeView[] trees = {TreeView1, TreeView2, TreeView3};
    Array.ForEach(trees, t => t.Visible = false);
    Array.ForEach(str.Split(',').Select(s => trees[Convert.ToInt32(s)]).ToArray(), t=>t.Visible = true);
      

  3.   

    已经解决问题!
     string str = "1,2";
            string[] arr = str.Split(',');
            int num = arr.Length;
            TreeView[] trees = { TreeView1, TreeView2, TreeView3 };
            for (int i = 0; i < num; i++)
            {
                trees[i].Visible = true;
            }
    这样就可以了。
    但是还有不明白的地方,还请jshi123指点。。
    Array.ForEach(trees, t => t.Visible = false);
    Array.ForEach(str.Split(',').Select(s => trees[Convert.ToInt32(s)]).ToArray(), ……
    这两个地方不明白。
      

  4.   

    =>是lambda表达式
    t => t.Visible = false 等价于:
    delegate(TreeView t) {
      t.Visible = false;
    }
      

  5.   

    这个我明白了,下面那个句子能解释下吗?
    Array.ForEach(str.Split(',').Select(s => trees[Convert.ToInt32(s)]).ToArray(), t=>t.Visible = true);
    这里不太懂。
    谢谢。
      

  6.   

    select是linq扩展方法
    http://msdn.microsoft.com/zh-cn/library/system.linq.enumerable.select.aspx