如何动态地为动态生成的标签/复选框提供自定义文本/命名约定?

发布于 2024-09-18 00:34:40 字数 543 浏览 2 评论 0原文

我试图通过 for 循环算法为动态生成的复选框命名。提出算法非常简单,但我无法给他们名称/复选框文本。我能想到的最好的办法就是为生成的所有内容提供一个类似的复选框文本。有没有办法给它们命名?

下面是我想出的代码

int x = ((((DropDownList1.SelectedIndex+1)*(DropDownList1.SelectedIndex+1))-(DropDownList1.SelectedIndex+1))/2)-1;
    Label1.Text = x+"";
    for (int i = 0; i < x; i++)
    {
        CheckBox cb = new CheckBox();
        cb.Text = "1 & 2";
        PlaceHolder1.Controls.Add(cb);
        PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
    }

非常感谢任何帮助。

干杯, 贾夫

I am trying to give names to my dynamically generated checkboxes through a for loop an algorithm. Coming up with the algorithm was pretty simple but I am unable to give them name/checkbox text. The best I could come up with was a similar checkbox text for everything that was generated. Is there a way to name them?

Below is the code I came up with

int x = ((((DropDownList1.SelectedIndex+1)*(DropDownList1.SelectedIndex+1))-(DropDownList1.SelectedIndex+1))/2)-1;
    Label1.Text = x+"";
    for (int i = 0; i < x; i++)
    {
        CheckBox cb = new CheckBox();
        cb.Text = "1 & 2";
        PlaceHolder1.Controls.Add(cb);
        PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
    }

Any help is greatly appreciated.

Cheers,
Jaf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

孤千羽 2024-09-25 00:34:40

您可以使用 for 语句的索引:

int x = ((((DropDownList1.SelectedIndex+1)*(DropDownList1.SelectedIndex+1))-(DropDownList1.SelectedIndex+1))/2)-1;
    Label1.Text = x+"";
    for (int i = 0; i < x; i++)
    {
        CheckBox cb = new CheckBox();
        cb.Text = String.Format("PrefixName{0}" , Convert.ToString(i+1));
        PlaceHolder1.Controls.Add(cb);
        PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
    }

You can use index of for statement:

int x = ((((DropDownList1.SelectedIndex+1)*(DropDownList1.SelectedIndex+1))-(DropDownList1.SelectedIndex+1))/2)-1;
    Label1.Text = x+"";
    for (int i = 0; i < x; i++)
    {
        CheckBox cb = new CheckBox();
        cb.Text = String.Format("PrefixName{0}" , Convert.ToString(i+1));
        PlaceHolder1.Controls.Add(cb);
        PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
    }
稳稳的幸福 2024-09-25 00:34:40

您应该使用控件的 ID 属性来指定名称。

像这样:

for (int i = 0; i < x; i++)
{
    CheckBox cb = new CheckBox();

    cb.ID = "checkBox" + i;

    PlaceHolder1.Controls.Add(cb);

    PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
}

每个控件中的 Text 属性都可以相同。
控件的名称 (ID) 不能相同。

You should assign the name using the control's ID property.

Like this:

for (int i = 0; i < x; i++)
{
    CheckBox cb = new CheckBox();

    cb.ID = "checkBox" + i;

    PlaceHolder1.Controls.Add(cb);

    PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
}

The Text property can be equal in every control.
You can't have identical names (IDs) for controls.

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