前台代码
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="添加label" /> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Panel ID="Panel1" runat="server" ViewStateMode="Enabled"></asp:Panel>
后台
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Session["demo"] = new List<Label>(); } }
string file_KB = FileUpload1.PostedFile.ContentLength.ToString() + "KB<br>";//获取文件大小 string file = FileUpload1.FileName;//获取上传文件名字 List<Label> list = Session["demo"] as List<Label>; Label l = new Label(); l.ID = "lbl" + (list.Count + 1); l.Text = file + file_KB; list.Add(l); Session["demo"] = list; foreach (Label item in list) { Panel1.Controls.Add(item); } int max = list.Count; List<string> lavlist = new List<string>(); foreach (Label item in list) { lavlist.Add(item.Text); }