ASP.NET如何在运行时设置控件的位置? [英] ASP.NET How do I set the position of a control at runtime?

查看:293
本文介绍了ASP.NET如何在运行时设置控件的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用以下代码在页面上添加标签,如何设置标签的位置(即右上角)?

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;
lbl.Font.Size = 10;
lbl.Font.Bold = false;
lbl.Font.Name = "Arial";
Page.Controls.Add(lbl);

谢谢

更新:我真的需要避免使用任何可以通过编辑服务器上运行的文件来更改的内容,这就是为什么我试图在运行时执行此操作.

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.

推荐答案

将PlaceHolder控件添加到页面上您要向其添加标签的位置,然后将该控件添加为PlaceHolder的子控件,例如.>

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>

然后...

LabelPlaceHolder.Controls.Add(lbl);

通常,在运行时动态添加控件是您要避免的事情.与通过内联属性设置样式一样(改为使用CSS).如果只希望在特定情况下显示标签,则将其添加到页面中,并将其 Visible 属性设置为 False ,然后将其设置为true以便查看它.

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.

这篇关于ASP.NET如何在运行时设置控件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆