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

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...