Asp.net
如何在佔位符中動態生成的標籤之間創建換行符?
Page_Load這是文件事件背後的程式碼中的以下程式碼:LinkButton linkButton = new LinkButton(); linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i; linkButton.ForeColor = Color.Blue; linkButton.Font.Bold = true; linkButton.Font.Size = 14; linkButton.Font.Underline = false; linkButton.Text = itemList[i].ItemTitle.InnerText; linkButton.Click += new EventHandler(LinkButton_Click); linkButton.Attributes.Add("LinkUrl",itemList[i].ItemLink.InnerText); PlaceHolder1.Controls.Add(linkButton); Label label = new Label(); label.ID = "LabelDynamicInPlaceHolder1Id" + i; label.ForeColor = Color.DarkGray; label.Text = itemList[i].ItemDescription.InnerText; PlaceHolder1.Controls.Add(label);我想在生成的每個控制項之間換行。
但是,您的換行問題的解決方案如下,如果您在 Page_Load 事件中執行此操作,那麼您的事件處理程序將無法工作並且您將遇到頁面生命週期問題。基本上,為了讓您的事件處理程序在 PostBack 上觸發,您確實需要在頁面生命週期的早期創建這些動態控制項。如果確實遇到此問題,請嘗試將程式碼移至 OnInit 方法。
LinkButton linkButton = new LinkButton(); linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i; linkButton.ForeColor = Color.Blue; linkButton.Font.Bold = true; linkButton.Font.Size = 14; linkButton.Font.Underline = false; linkButton.Text = itemList[i].ItemTitle.InnerText; linkButton.Click += new EventHandler(LinkButton_Click); linkButton.Attributes.Add("LinkUrl",itemList[i].ItemLink.InnerText); PlaceHolder1.Controls.Add(linkButton); //Add This PlaceHolder1.Controls.Add(new LiteralControl("<br />")); Label label = new Label(); label.ID = "LabelDynamicInPlaceHolder1Id" + i; label.ForeColor = Color.DarkGray; label.Text = itemList[i].ItemDescription.InnerText; PlaceHolder1.Controls.Add(label);