Associate a shared list with ASP.NET DropDownList

I am trying to associate a generic list with a DropDownList, and I'm not sure how to proceed.

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;
    }
+3
source share
3 answers

Fix! I should have added this:

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

Your problem may be related to the binding, which you could only bind if you did not send back!

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

What problem did you encounter?

By the way, DataValueField must be unique. Perhaps this can cause a problem either with binding, or after feedback.

0
source

Source: https://habr.com/ru/post/1793819/


All Articles