开发者

ASP.NET How do I set the position of a control at runtime?

开发者 https://www.devze.com 2022-12-27 04:47 出处:网络
I\'m adding a label to a page using the code below, how would I set the position of the label (i.e top right)?

I'm adding a label to a page using the code below, how would I set the position of the label (i.e top right)?

Label lbl = new Label();
lbl.Text = "Test";
lbl.ForeColor = System.Drawing.Color.Black;
lb开发者_运维问答l.Font.Size = 10;
lbl.Font.Bold = false;
lbl.Font.Name = "Arial";
Page.Controls.Add(lbl);

Thanks

Update: I really need to avoid using anything that can be altered via editing a file running on the server which is why I'm trying to do this at runtime.


Add a PlaceHolder control to your page in the position you want to add the label to and then add the control as a child control of the PlaceHolder eg.

<asp:PlaceHolder ID="LabelPlaceHolder" runat="server">
</asp>

And then...

LabelPlaceHolder.Controls.Add(lbl);

Generally, though, dynamically adding controls at run-time is something you want to avoid. As is setting styling through in-line properties (use CSS instead). If you only want the Label to appear under specific circumstances then add it to the page with it's Visible property set to False and then set it to true to when you want to see it.


I'd suggest you do all formatting etc. with CSS - so, at runtime all you would specify is a css class for the control and let the browser do the rest.

0

精彩评论

暂无评论...
验证码 换一张
取 消