将通用列表绑定到 ASP.NET DropDownList

发布于 2024-10-18 10:43:31 字数 2589 浏览 1 评论 0原文

我正在尝试将通用列表绑定到 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;
    }

I am trying to bind generic list to DropDownList an i am not sure how to continue.

here is my code:

   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;
    }

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

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

发布评论

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

评论(3

想你的星星会说话 2024-10-25 10:43:31

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

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

Fix it!
I had to add this:

public class Paths
    {
        public string orignalPathName { get; set; }
        public int orignalPathId { get; set; }
        public string newPathName { get; set; }
    }
酒解孤独 2024-10-25 10:43:31

您的问题可能来自于绑定前景,只有在不回发时您才可能绑定!

If(!Page.IsPostBack)
{
    List<Paths> paths = new List<Paths>();
    paths = GetOriginalPaths();
    DropDownList1.DataSource = paths;
    DropDownList1.DataTextField = "orignalPathName";
    DropDownList1.DataValueField = "orignalPathId";
    DropDownList1.DataBind();
}

Your problem might be from the bind prospective you might bind only if not post back!!

If(!Page.IsPostBack)
{
    List<Paths> paths = new List<Paths>();
    paths = GetOriginalPaths();
    DropDownList1.DataSource = paths;
    DropDownList1.DataTextField = "orignalPathName";
    DropDownList1.DataValueField = "orignalPathId";
    DropDownList1.DataBind();
}
逆夏时光 2024-10-25 10:43:31

您到底遇到了什么问题?

顺便说一句,DataValueField 应该是唯一的。事实上,它可能会导致绑定或回发出现问题。

What problem are you encountering exactly?

By the way, the DataValueField should be unique. Aas it is it could cause an issue either with the bind, or on post-back.

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