asp.net - 将通用列表绑定(bind)到 ASP.NET DropDownList

标签 asp.net drop-down-menu generic-list

我正在尝试将通用列表绑定(bind)到 DropDownList,但我不知道如何继续。

这是我的代码:

   protected void Page_Load(object sender, EventArgs e)
    {
        List<Paths> paths = new List<Paths>();
        paths = GetOriginalPaths();
        DropDownList1.DataSource = paths;
        DropDownList1.DataTextField = "orignalPathName";
        DropDownList1.DataValueField = "orignalPathId";
        DropDownList1.DataBind();
    }


    public class Paths
        {
            public string orignalPathName;
            public int orignalPathId;
            public string newPathName;
        }
        public static List<Paths> GetOriginalPaths()
        {
            return PrepareOriginalPathsData();
        }
        public static List<Paths> PrepareOriginalPathsData()
        {
            List<Paths> objPaths = new List<Paths> { 
                new Paths{orignalPathName = "comp1", orignalPathId= 1} ,
                new Paths{orignalPathName = "comp1", orignalPathId= 1} ,
                new Paths{orignalPathName = "comp1", orignalPathId= 1} ,
                 new Paths{orignalPathName = "comp2", orignalPathId= 2} ,
                new Paths{orignalPathName = "comp3", orignalPathId= 3} ,
                new Paths{orignalPathName = "comp4", orignalPathId= 4}
          };
            return objPaths;
        }
        public static List<Paths> GetNewPaths(int orignalPathId)
    {
        List<Paths> lstNewPaths = new List<Paths>();
        Paths objnewPaths = null;
        var newPath = (from np in PrepareNewPathsData() where np.orignalPathId == orignalPathId select new { newpathname = np.newPathName, orgId = np.orignalPathId });
        foreach (var np in newPath)
          {
              objnewPaths = new Paths();
              objnewPaths.orignalPathId = np.orgId;
              objnewPaths.newPathName = np.newpathname;
              lstNewPaths.Add(objnewPaths);
          }
          return lstNewPaths;
    }
        public static List<Paths> PrepareNewPathsData()
    {
        List<Paths> objNewPaths = new List<Paths> { 
                new Paths{newPathName = "part1", orignalPathId= 1} ,
                new Paths{newPathName = "part1", orignalPathId= 1} ,
                new Paths{newPathName = "part1", orignalPathId= 1} ,
                new Paths{newPathName = "part3", orignalPathId= 2} ,
                new Paths{newPathName = "part4", orignalPathId= 3} ,
                new Paths{newPathName = "part5", orignalPathId= 4} ,

          };
        return objNewPaths;
    }

最佳答案

修复它! 我必须添加这个:

public class Paths
    {
        public string orignalPathName { get; set; }
        public int orignalPathId { get; set; }
        public string newPathName { get; set; }
    }

关于asp.net - 将通用列表绑定(bind)到 ASP.NET DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5076559/

相关文章:

c# - 将 IEnumerable<T> 转换为 JSON 对象

c# - 将对象转换到通用列表中以获取项目列表

c# - GridView 中 DropDownList 中的 Eval()

html - 如何更改 Bootstrap 下拉列表 :before triangle to different shape

asp.net - LINQ - WHERE 子句中的 CASE 语句

c# - 10401 订单总计无效 ASP.NET C#

JQuery Select2 下拉 onchange 选项文本颜色不改变

c# - 将通用列表<string> 转换为 byte[ ]

javascript - ajax 调用 mvc 操作中的空数据

c# - 根文件夹的 asp.net 处理程序