使用数组填充多个DropDownList

时间:2015-11-23 09:11:21

标签: c# asp.net xml

我尝试填充多个DropDownLists。在我的UI中,有多个选项卡,每个选项卡中始终使用相同的DropDownList。我使用一个数组然后使用foreach来填充我的DropDownList:

public void DropDownList()
    { 
        string filePath = Server.MapPath("~/Datas.xml");
        using (DataSet ds = new DataSet())
        {
            DropDownList[] ddls = new DropDownList[]{
               DropDownList_1, DropDownList2,
               DropDownList4, DropDownList5,
               DropDownList7
            };

            foreach (DropDownList ddl in ddls)
            {
                ds.ReadXml(filePath);
                ddl.DataSource = ds;
                ddl.DataTextField =  "name";
                ddl.DataValueField = "id";
                ddl.DataBind();
            }        
        }
    }

这是XML:

<?xml version="1.0" encoding="utf-8" ?>
    <fields>
      <cafs>
        <caf>
          <id>1</id>
          <name>TEST18</name>
        </caf>
        <caf>
          <id>2</id>
          <name>TEST28</name>
        </caf>
        <caf>
          <id>3</id>
          <name>TEST36</name>
        </caf>
        <caf>
          <id>4</id>
          <name>TEST45</name>
        </caf>
        <caf>
          <id>5</id>
          <name>TOTAL</name>
        </caf>
      </cafs>  
    </fields>

错误图片:enter image description here

但我在line ddl.DataBind();

上收到错误

它表示System.Data.DataRowView have no 'name' property.(在InnerException中为null)

我不明白为什么因为之前有效。数据集已加载,我可以在Visualizer中显示它。

可能出现此错误的任何想法?

0 个答案:

没有答案
相关问题