开发者

How do I bind a list to a ddl?

I have a list and I am trying to add the data to the ddl. It returns data, (namespa开发者_如何转开发ce.List). But there is something I am missing... any suggestions?

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


All you're missing is to tell the dropdown what property of getBranch to show as text and what to use as value:

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

ok, here's how the code should look like (i think):

your Branch class properties:

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜