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

前端 未结 3 914
谎友^
谎友^ 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: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("
    ")); Label label = new Label(); label.ID = "LabelDynamicInPlaceHolder1Id" + i; label.ForeColor = Color.DarkGray; label.Text = itemList[i].ItemDescription.InnerText; PlaceHolder1.Controls.Add(label);

提交回复
热议问题