2009-11-16 93 views
1

我有一个下拉列表框,其中一个值是其他值。我想把这些价值转移到最后。请帮我解决一下这个。我使用的代码如下将新值添加到下拉列表框中

ddlAssetsCountryOthersone.DataSource = lstIMAssetsByCountryOthers; 
ddlAssetsCountryOthersone.DataTextField = "AssetDescription"; 
ddlAssetsCountryOthersone.DataValueField = "AssetDescription"; 
ddlAssetsCountryOthersone.DataBind(); 
ddlAssetsCountryOthersone.Items.Remove(
      ddlAssetsCountryOthersone.Items.FindByValue(
       Constants.PhilosophyAndEmphasis.Other.ToString())); 

我怎么能在最后

+1

我敢打赌,如果任何人明白你想要完成的! – TheVillageIdiot 2009-11-16 12:03:13

回答

4

添加其他回下拉列表中选择数据绑定后,试试这个:

ddlAssetsCountryOthersone.Items.Add(new ListItem("Others", "Others")); 

顺便说一句如果你使用插入方法,你可以插入你想要的位置到你想要的位置。例如下面的代码添加其他选项到4阶:

ddlAssetsCountryOthersone.Items.Insert(4, new ListItem("Others", "Others")); 
1

如果您在下拉列表是你不会是数据绑定能够在DataBind()电话后,项目添加到您的控制,如果你之前,他们将它们添加无论如何,当你拨打DataBind()将被清除。

数据绑定发生后,您可以使用InsertAdd,并且您可以指定要插入的项目的索引。 Insert用于在特定的位置插入,Add将追加到了谷底,因为你的情况:

//after databind 

//example with insert - (-1 as list is zero based) 
ddlMyList.Items.Insert(noOfItems-1, "Other"); 

OR

//add 
ddlMyList.Items.Add("Other"); 
0

或者:

ListItem li = ddlAssetsCountryOthersone.FindByValue(Constants.PhilosophyAndEmphasis.Other.ToString())); 
ddlAssetsCountryOthersone.Remove(li); 
ddlAssetsCountryOthersone.Add(li); 

这应该工作,请测试 - 并按照JohnIdol的建议更换Add with Insert ...

1

,你可以尝试像

ListItem li = new ListItem("text", "value"); 
    yourDropdown.Items.Insert(yourDropdown.Items.Count, li); 

如果您在下拉菜单中5个项目,它将返回5,因为插入指数从0开始

0

绑定时,它要容易得多添加/删除的项目列表是数据绑定比在数据绑定发生后添加/删除项目。

我会创建一个围绕lstIMAssetsByCountryOthers的包装,它会将必要的更改转换为新的IEnumberable并将该新对象返回为数据绑定。

相关问题