加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何在占位符中的动态生成的标签之间创建换行符?

发布时间:2020-12-16 00:31:06 所属栏目:asp.Net 来源:网络整理
导读:这是代码在下面的代码在文件的Page_Load事件中: LinkButton linkButton = new LinkButton(); linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i; linkButton.ForeColor = Color.Blue; linkButton.Font.Bold = true; linkButton.Font.Size = 14; lin
这是代码在下面的代码在文件的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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读