以下代码位于文件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事件中执行此操作,那么您的事件处理程序将无法工作,您将遇到Page Life Cycle问题。基本上,为了让你的事件处理程序在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);

其他提示

另一个解决方案是你可以将每个控件添加到一个Panel,它将在<div>中呈现它们,从而产生你正在寻找的效果。

对我而言,这将更具动态性,因为如果你隐藏任何控件,div将会崩溃并且不会留下空行。

    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);

    //Add control to a panel, add panel to placeholder
    Panel lbPan = new Panel();
    lbPan.Controls.Add(linkButton); 
    PlaceHolder1.Controls.Add(lbPan);


    Label label = new Label();
    label.ID = "LabelDynamicInPlaceHolder1Id" + i;
    label.ForeColor = Color.DarkGray;
    label.Text = itemList[i].ItemDescription.InnerText;

    //Add control to a panel, add panel to placeholder
    Panel lblPan = new Panel();
    lblPan.Controls.Add(label); 
    PlaceHolder1.Controls.Add(lblPan);

如何:以编程方式将控件添加到ASP.NET网页

  

在某些情况下,您可能想要   创建静态文本和控件。   要创建静态文本,您可以使用Literal或Label Web服务器   控制即可。然后,您可以添加这些   像你一样控制容器   任何其他控制。有关信息   关于创建的控件中的视图状态   在运行时,请参阅Dynamic Web Server   控制和查看状态。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top