循环插入label的值

      protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["demo"] = new List<String>();
                ////Session["demo"] = new List<Label>();
            }
        }
        public string ls;//定义全局变量
        protected void Button2_Click(object sender, EventArgs e)
        {

                string file_KB = FileUpload1.PostedFile.ContentLength.ToString() + "KB<br>";//获取文件大小
                string file = FileUpload1.FileName;//获取上传文件名字

                string URL = "http://amid01110/456/" + file + " ";
                List<string> list = Session["demo"] as List<string>;
                Session["demo"] = list;
                string lntext = "<a  href='" + URL + "'  >" + file + "</a>" + "大小" + file_KB ;
               
                list.Add(lntext);//上面定义一个string类型的List把值传进去并保存List给Session
                //string lss = "";
                foreach (string item in list)//下面循环List里面的值给页面定义的Label并把List值循环传给
                {
                    Label l = new Label();
                    l.ID = "lbl";
                    l.Text = item;
                    
                   Panel1.Controls.Add(l);
                   //lss += item;
                   ls += item;//把值循环给全局变量
                }

               
                int max = list.Count;
              
        }
    }
原文地址:https://www.cnblogs.com/914556495wxkj/p/3566162.html