循环浏览页面的标签以分配文本

发布于 2024-11-17 15:17:07 字数 433 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-11-24 15:17:07

如果您将它们放在容器中,我认为您可以执行如下操作:

For Each lbl As Control In Grid1.Children
  If TypeOf lbl Is Label Then
    'your logic
  End If
Next

但是,我只在 silverlight 中尝试过此操作,所以我不确定它是否有效,或者将它们全部放入容器中是否适合您的情况。

If you have them in a container, i think you can do something like this below:

For Each lbl As Control In Grid1.Children
  If TypeOf lbl Is Label Then
    'your logic
  End If
Next

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.

平生欢 2024-11-24 15:17:07

一种方法是使用 findcontrol 方法。这会很有效,因为所有标签都以“lbl0”、“lbl1”...约定命名。

**开始循环:

int index = 0;

string currentLabel = "lbl" + index.ToString();

index++;

Control myControl1 = FindControl(currentLabel);

// cast control to type: (label)

// apply text from reader**

试一试。希望能成功

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:

int index = 0;

string currentLabel = "lbl" + index.ToString();

index++;

Control myControl1 = FindControl(currentLabel);

// cast control to type: (label)

// apply text from reader**

Give that a shot. Hope it works out

抚笙 2024-11-24 15:17:07

我过去用过这个,并且刚刚测试过。

您可以这样做,因为每个页面都有一个表单

HtmlForm form1 = (HtmlForm)Page.FindControl("ContentPlaceHolder1");

for (int i = 1; i <= 3; i++) {
    ((TextBox)form1.FindControl("label" + i)).Text = "This is label number " + i;
}

如果您有一个母版页,请将第一行更改为此

ContentPlaceHolder ph = (ContentPlaceHolder)Page.FindControl("ContentPlaceHolder1");

I have used this in the past and I just tested it out.

You can do this because every page has a form

HtmlForm form1 = (HtmlForm)Page.FindControl("ContentPlaceHolder1");

for (int i = 1; i <= 3; i++) {
    ((TextBox)form1.FindControl("label" + i)).Text = "This is label number " + i;
}

If you have a master page change the first line to this

ContentPlaceHolder ph = (ContentPlaceHolder)Page.FindControl("ContentPlaceHolder1");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文