声明同一个表的多个实例

时间:2021-12-14 14:04:22

I have a table that needs 27 drop down menus in 27 cells to accept user input. Currently I am declaring all 27 of them like this:

我有一个表,需要27个单元格中的27个下拉菜单来接受用户输入。目前,我正在像这样宣布这27个国家:

DropDownList DropList1 = new DropDownList();
DropList1.ID = "TrendList1";
DropList1.AutoPostBack = true;
DropList1.SelectedIndexChanged += new EventHandler(this.Selection_Change);
DropList1.DataSource = CreateDataSource();
DropList1.DataTextField = "ColorTextField";
DropList1.DataValueField = "ColorValueField";
DropList1.DataBind();

DropDownList DropList2 = new DropDownList();
DropList2.ID = "TrendList2";
DropList2.AutoPostBack = true;
DropList2.SelectedIndexChanged += new EventHandler(this.Selection_Change);
DropList2.DataSource = CreateDataSource();
DropList2.DataTextField = "ColorTextField";
DropList2.DataValueField = "ColorValueField";
DropList2.DataBind();

etc...

However I know there has to be a better way than the brute force code I have written. Unfortunately I am new to web programming and I haven't been able to figure out a better way to do this.

但是我知道一定有比我编写的蛮力代码更好的方法。不幸的是,我是网络编程新手,我还没能想出更好的方法来做这个。

Any advice is appreciated.

任何建议都是感激。

Regards.

的问候。

1 个解决方案

#1


2  

var data = CreateDataSource();

for(x = 1; x < 28; x++)
{
    DropDownList dl = new DropDownList();
    dl.ID = "TrendList" + x.ToString();
    dl.AutoPostBack = true;
    dl.SelectedIndexChanged += new EventHandler(this.Selection_Change);
    dl.DataSource = data;
    dl.DataTextField = "ColorTextField";
    dl.DataValueField = "ColorValueField";
    dl.DataBind();
    // add it to the cell here too
}

#1


2  

var data = CreateDataSource();

for(x = 1; x < 28; x++)
{
    DropDownList dl = new DropDownList();
    dl.ID = "TrendList" + x.ToString();
    dl.AutoPostBack = true;
    dl.SelectedIndexChanged += new EventHandler(this.Selection_Change);
    dl.DataSource = data;
    dl.DataTextField = "ColorTextField";
    dl.DataValueField = "ColorValueField";
    dl.DataBind();
    // add it to the cell here too
}