I have a file upload controller to ftp server. I wants to retrieve the file names of each uploaded file and wants to display it on the same page as a linkbutton. so that i wants to add linkbuttons dynamically to the page. i have done like this. but when uploading more than one file it replaces the previously added links and only shows the last added file as linkbutton.
protected void UploadButton_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUploadControl.FileName);
Session["f_name"] = filename;
Panel1.Controls.Add(new LiteralControl("<div>"));
LinkButton lb = new LinkButton();
lb.Text = filename;
lb.ID = filename;
Session["lb_pdf"]=lb.Text;
string pdf=(string)Session["lb_pdf"];
Panel1.Controls.Add(lb);
Panel1.Controls.Add(new LiteralControl("</div>"));
lb.PostBackUrl = "Default2.aspx";
how can i add multiple linkbuttons dynamically? please help
You could use a List<string>
instead of a single string:
List<string> allFiles = new List<string>();
if(Session["f_name"] != null)
allFiles = (List<string>)Session["f_name"];
else
Session["f_name"] = allFiles;
allFiles.Add(filename);
foreach(string fileName in allFiles)
{
// now create the LinkButtons ...
}
Note that you have to recreate all dynamically created controls on every postback. So if you can postback from this page without using the UploadButton
you should use Page_Init
or Page_Load
to recreate the old LinkButtons
instead. Thats also the better place in the life-cycle.
See: TRULY UNDERSTANDING DYNAMIC CONTROLS (Adding Dynamic Controls to the Control Tree)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.