如何将列表绑定到ddl?

时间:2009-10-26 20:11:24

标签: c# .net asp.net

我有一个列表,我正在尝试将数据添加到ddl。它返回数据,(namespace.List)。但是我有些遗漏......有什么建议吗?

public List<getBranch> Branch { get; private set; }
...                           
getBranch(user.code);
ddlOption.DataSource = Branch;                        
ddlOption.DataBind();

1 个答案:

答案 0 :(得分:5)

你所遗漏的只是告诉下拉列表getBranch的哪些属性显示为文本以及用作值的内容:

ddlOption.DataTextField = "propertyOfgetBranchToShowAsText";
ddlOption.DataValueField = "propertyOfgetBranchToUseAsValue";

好的,这是代码的样子(我认为):

您的分类属性:

public int BranchValue {get;set;}
public string BranchText {get;set;}

...

public List<Branch> branchesToShow { get; private set; }
...                           
branchesToShow = getBranch(user.code); //get the list of branches
ddlOption.DataTextField = "BranchText"
ddlOption.DataValueField = "BranchValue";
ddlOption.DataSource = branchesToShow;                        
ddlOption.DataBind();