Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create line breaks between dynamically generated labels in a placeholder?

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.

like image 904
Maddy.Shik Avatar asked May 06 '09 15:05

Maddy.Shik


People also ask

How to add line break dynamically in asp net?

This is the code below in code behind file's Page_Load event: LinkButton linkButton = new LinkButton(); linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i; linkButton. ForeColor = Color.

How do I preserve line breaks in textarea?

If you want your text to overflow the parent's boundaries, you should use pre as your CSS whitespace property. Using white-space: pre wraps still preserves newlines and spaces. Enjoy!

How do you break a line in a tooltip title?

Just use the entity code 
 for a linebreak in a title attribute. Save this answer.


2 Answers

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);
like image 74
Eoin Campbell Avatar answered Oct 24 '22 18:10

Eoin Campbell


Another solution is that you could add each control to a Panel, which will render them each in a <div> resulting in the effect you're looking for.

To me this would be more dynamic because if you hide any of the controls the div will collapse and not leave empty lines.

    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);
like image 30
Thymine Avatar answered Oct 24 '22 18:10

Thymine