首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在占位符中动态生成的标签之间创建换行符?

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

Stack Overflow用户
提问于 2009-05-06 15:52:14
回答 2查看 43K关注 0票数 16

这是文件的Page_Load事件背后的代码中的代码:

代码语言:javascript
复制
        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);

我想要在生成的每个控件之间换行。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-05-06 15:59:40

换行符问题的解决方案如下所示,但是,如果您在Page_Load事件中执行此操作,则事件处理程序将无法工作,并且会遇到页面生命周期问题。基本上,为了让事件处理程序在PostBack上触发,您确实需要在页面生命周期的早期创建这些动态控件。如果遇到此问题,请尝试将代码移动到OnInit方法。

代码语言:javascript
复制
    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);
票数 40
EN

Stack Overflow用户

发布于 2012-06-08 02:43:34

另一种解决方案是,您可以将每个控件添加到面板中,这将在<div>中呈现每个控件,从而产生您想要的效果。

对我来说,这会更具动态性,因为如果您隐藏任何控件,div就会折叠,并且不会留下空行。

代码语言:javascript
复制
    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);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/830304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档