如何创建动态生成的标签之间换行符的占位符? [英] How to create line breaks between dynamically generated labels in a placeholder?

查看:248
本文介绍了如何创建动态生成的标签之间换行符的占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是低于code在隐藏文件中的的Page_Load 事件code:

This is the code below in code behind file's Page_Load event:

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

我要生成的每个控制之间的换行。

I want a line break between each control generated.

推荐答案

解决您的线路中断的问题是低于不过,如果你在Page_Load事件这样做,那么你的事件处理程序将无法正常工作,你的打算碰上页生命周期的问题。基本上,为了您的事件处理程序,以火上回发,你真的需要更早在页面生命周期创建这些动态控件。请尝试移动code到OnInit方法,如果你遇到了这个问题。

Solution to your Line Break issue is below however, if you're doing this in the Page_Load event, then your event handlers won't work and your going to run into Page Life Cycle issues. Basically, in order for your event handlers to fire on PostBack, you really need to be creating these dynamic controls earlier in the Page Life Cycle. Try moving your code to the OnInit method if you do run into this problem.

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

这篇关于如何创建动态生成的标签之间换行符的占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆