将通用列表绑定到 ASP.NET DropDownList
我正在尝试将通用列表绑定到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
修复它!
我必须添加这个:
Fix it!
I had to add this:
您的问题可能来自于绑定前景,只有在不回发时您才可能绑定!
Your problem might be from the bind prospective you might bind only if not post back!!
您到底遇到了什么问题?
顺便说一句,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.