English 中文(简体)
Adding a new value to the drop down list box
原标题:

I have a drop down list box which has one of the values to be others. I want to move these value to the last. Please help me with this. The code i am using is as follows

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

How can i add the others back to the drop down list in the last

最佳回答

try this after your databind :

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

By the way if you use Insert method, you can insert the item you want to the position you want. For example the code below adds the Other option to the 4th order :

ddlAssetsCountryOthersone.Items.Insert(4, new ListItem("Others", "Others"));
问题回答

you can try like

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

If you have 5 items in dropdown, it will return 5, since the insert index start from 0

If you the dropdownlist is databound you will not be able to add items to your control after the DataBind() call, and if you add them before they will be cleared anyway whe you call DataBind().

You can use Insert or Add after the data binding takes place, and you can specify the index of the item you re inserting. Insert is used to insert at the specific location, Add will append to the bottom, as in your case:

//after databind

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

OR

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

Or:

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

That should work, please test - and replace Add with Insert as per JohnIdol s suggestion...

When databinding, it is far easier to add/remove items from the list that is being databound than it is to add/remove items after the data binding takes place.

I would create a wrapper around lstIMAssetsByCountryOthers that makes the necessary changes into a new IEnumberable and returns that new object to be databound.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签