자리 표시 자에서 동적으로 생성 된 레이블 사이에 라인 브레이크를 만드는 방법은 무엇입니까?
-
06-07-2019 - |
문제
이것은 파일 뒤에있는 코드의 아래 코드입니다. Page_Load
이벤트:
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);
생성 된 각 컨트롤 사이의 라인 브레이크를 원합니다.
해결책
라인 브레이크 문제에 대한 솔루션은 아래에 있습니다. 그러나 Page_Load 이벤트 에서이 작업을 수행하는 경우 이벤트 핸들러가 작동하지 않으며 페이지 수명주기 문제가 발생합니다. 기본적으로, 이벤트 핸들러가 Postback에서 발사하려면 페이지 수명주기 초반에 이러한 동적 컨트롤을 만들어야합니다. 이 문제가 발생하면 코드를 Oninit 메소드로 옮기십시오.
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);
다른 팁
또 다른 해결책은 각 컨트롤을 패널에 추가 할 수 있다는 것입니다. <div>
당신이 찾고있는 효과가 있습니다.
나에게 이것은 컨트롤을 숨기면 div가 무너지고 빈 줄을 남기지 않기 때문에 이것은 더 역동적 일 것이다.
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);
방법 : ASP.NET 웹 페이지에 컨트롤 추가 프로그래밍 방식으로 컨트롤
경우에 따라 정적 텍스트와 컨트롤을 모두 만들 수 있습니다. 정적 텍스트를 만들려면 리터럴 또는 레이블 웹 서버 컨트롤을 사용할 수 있습니다.. 그런 다음 다른 컨트롤과 마찬가지로 컨테이너에 이러한 컨트롤을 추가 할 수 있습니다. 실행 시간에 생성 된 컨트롤의 뷰 상태에 대한 정보는 동적 웹 서버 컨트롤 및 뷰 상태를 참조하십시오.
제휴하지 않습니다 StackOverflow