多个数据源到单个下拉列表

时间:2012-08-01 08:28:59

标签: asp.net

如何将多个数据源设置为单个下拉列表?

 DrpPosition.ClearSelection();
        DrpPosition.DataSource = AdvertizeManager.GetList("", "", "", "");
        DrpPosition.DataTextField = "PNAME";
        DrpPosition.DataValueField = "ADID";
        DrpPosition.DataBind();
        DrpPosition.DataSource = CategoryManager.GetList("", "", "", "");
        DrpPosition.DataTextField = "NAME";
        DrpPosition.DataValueField = "CATID";
        DrpPosition.DataBind();

2 个答案:

答案 0 :(得分:1)

您可以使用LINQ轻松解决此问题:

        var advertizes = AdvertizeManager.GetList...
        var categories = CategoryManager.GetList....

        // create your annonymous type
        var customAdvertizes = from advertize in advertizes
                select new
                {
                    Value = advertize.ADID,
                    Text = advertize.PNAME
                };

        var finalQuery = customAdvertizes.Concat(
            from category in categories
            select new
            {
                Value = category.CATID,
                Text = category.NAME
            }
        );

然后将其绑定到您的DropDownList

    DrpPosition.DataSource = finalQuery;
    DrpPosition.DataTextField = "Text";
    DrpPosition.DataValueField = "Value";
    DrpPosition.DataBind();

答案 1 :(得分:0)

这是不可能的,但您可以在绑定之前合并数据源。

或者您可以使用

foreach(var item in yourDataSourceComplete)//yourDataSourceComplete = List1 + List2
{ 
  .....
  DrpPosition.Items.Add(Yourkey, YourValue);
}