如何在占位符中动态生成的标签之间创建换行符?
这是文件 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);
我希望在生成的每个控件之间有一个换行符。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是换行问题的解决方案,但是,如果您在 Page_Load 事件中执行此操作,那么您的事件处理程序将无法工作,并且您将遇到页面生命周期问题。 基本上,为了让事件处理程序在回发时触发,您确实需要在页面生命周期的早期创建这些动态控件。 如果确实遇到此问题,请尝试将代码移至 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.
另一个解决方案是您可以将每个控件添加到面板中,这将在
中呈现它们,从而产生您正在寻找的效果。
对我来说,这会更加动态,因为如果您隐藏任何控件,div 将折叠并且不会留下空行。
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.
如何:以编程方式向 ASP.NET 网页添加控件
How to: Add Controls to an ASP.NET Web Page Programmatically