下拉列表控件数组具有相同的值

时间:2015-05-20 14:54:12

标签: c# controls

我有一系列Dropdownlist控件。当我将所选值设置为1时,它会为所有值设置它们。我做错了什么?

Dropdownlist[] _ddl = new Dropdownlist[7];

for(int i=0;i<=6;i++)
{
    _dll[i] = new Dropdownlist();
    _dll[i].ID = "dropdown" + i;
    _ddl[i].AutoPostBack = true;
    _ddl[i].Items.AddRange(GradeLevels);
    if (Grade == "A")
    _ddl[i].Items.FindByValue("A").Selected = true;

}

4 个答案:

答案 0 :(得分:1)

我建议你使用

    String Grade = "A";
    string[] GradeLevels = new string[] { "A", "B", "C" };
    DropDownList _ddl = new DropDownList();
    _ddl.AutoPostBack=true;
    for (int i = 0; i < GradeLevels.Length; i++)
    {
        _ddl.Items.Add(new ListItem(GradeLevels[i], GradeLevels[i]));
    }
        _ddl.Items.FindByValue(Grade).Selected = true;
    PlaceHolder1.Controls.Add(_ddl);

答案 1 :(得分:0)

试试这个:

    Dropdownlist[] _ddl = new Dropdownlist[6];

    for(int i = 0; i < 6; i++)
    {
        _dll[i] = new Dropdownlist();
        _dll[i].ID = "dropdown" + i;
        _ddl[i].AutoPostBack = true;
        _ddl[i].Items.AddRange(GradeLevels);       
    }

    // set the selected item in the first list
    if (Grade == "A")
        _ddl[0].Items.FindByValue("A").Selected = true;

答案 2 :(得分:0)

您必须创建GradeLevels对象的克隆副本,并向每个Dropdownlist添加该对象的不同副本。

答案 3 :(得分:0)