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

I am adding a shortcut to a page using the code below, how would I set the position of the label (ie top right)?

Label lbl = new Label();
lbl.Text = "Test";
lbl.ForeColor = System.Drawing.Color.Black;
lbl.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 changed by editing a file running on the server, so I'm trying to do this at runtime.

+2


a source to share


2 answers


Add a PlaceHolder element to your page at the position where you want to add the label, and then add the control as a child of the PlaceHolder, for example.

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

      

And then...



LabelPlaceHolder.Controls.Add(lbl);

      

Generally, dynamically adding controls at runtime is what you want to avoid. As is the styling through inline properties (use CSS instead). If you only want the label to appear under certain circumstances, add it to the page with its Visible

value set to False

, and then set it to true if you want to see it.

+3


a source


I would advise you to do all the formatting, etc. using CSS - so at runtime whatever you have specified is the css class for the control and let the browser do the rest.



+2


a source







All Articles