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

前端 未结 3 841
谎友^
谎友^ 2021-02-19 01:45

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

        LinkButton linkButton = new LinkButton();
        linkButton.ID = \"LinkButto         


        
相关标签:
3条回答
  • 2021-02-19 02:09

    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);
    
    0 讨论(0)
  • 2021-02-19 02:11

    How to: Add Controls to an ASP.NET Web Page Programmatically

    In some instances, you might want to create both static text and controls. To create static text, you can use either a Literal or a Label Web server control. You can then add these controls to the container as you would any other control. For information about view state in controls created at run time, see Dynamic Web Server Controls and View State.

    0 讨论(0)
  • 2021-02-19 02:28

    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);
    
    0 讨论(0)
提交回复
热议问题