循环浏览页面的标签以分配文本
我的 aspx 页面上有大约 20 个标签,其 ID 为 lbl1、lbl2....lbl20,文本由 SqlServer 表驱动。有没有简单的方法可以循环浏览页面上的所有标签并评估阅读器的文本。 我做了一些类似的事情,但它不起作用。
SqlDataReader Reader = new SqlDataReader();
int i = 0;
while(Reader.read())
{
label lbl = new label();
lbl.ID = "label" + i;
lbl.text = Reader["ColumnName"].ToString();
}
有没有其他方法可以循环遍历所有标签并为其分配文本?
I have some 20 labels on my aspx page for which the IDs are lbl1,lbl2....lbl20 and text is driven by SqlServer table. Is there any easy way to loop through all the labels on the page and assing the text from reader.
I did some thing like but it doesn't work.
SqlDataReader Reader = new SqlDataReader();
int i = 0;
while(Reader.read())
{
label lbl = new label();
lbl.ID = "label" + i;
lbl.text = Reader["ColumnName"].ToString();
}
Is there any other method through which I can loop through all the labels and assign text for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您将它们放在容器中,我认为您可以执行如下操作:
但是,我只在 silverlight 中尝试过此操作,所以我不确定它是否有效,或者将它们全部放入容器中是否适合您的情况。
If you have them in a container, i think you can do something like this below:
I have only tried this in silverlight however, so i'm not sure it works, or if putting them all in a container is practical in your case.
一种方法是使用 findcontrol 方法。这会很有效,因为所有标签都以“lbl0”、“lbl1”...约定命名。
**开始循环:
试一试。希望能成功
One way to do this is use the findcontrol method. This would work well because all your labels are named with a "lbl0", "lbl1"... convention.
**start looping:
Give that a shot. Hope it works out
我过去用过这个,并且刚刚测试过。
您可以这样做,因为每个页面都有一个表单
如果您有一个母版页,请将第一行更改为此
I have used this in the past and I just tested it out.
You can do this because every page has a form
If you have a master page change the first line to this