locked
Dropdown list RRS feed

  • Question

  • User1057158394 posted

    I have a collection with following values that is bound to a drop down list.

    1.00, 1.25, 1.50, 1.75, 2.00, 2.25, 2.50, 2.75, 3.001 3.25, 3.50, 3.75, 4.00…so on

    When data is bound to the ddl control, I want to hide or possibly ignore the items that are 1.25, 1.75 and so on. I do not want to hard code and I would like to do that dynamically so that when new values are available in the source collection it works just the same way without any change to the system.

    How can I achieve that?

    Thanks.

    Tuesday, February 21, 2017 7:52 PM

Answers

  • User281315223 posted

    When data is bound to the ddl control, I want to hide or possibly ignore the items that are 1.25, 1.75 and so on.

    How are you planning on defining which items should and should not be hidden? If there is a specific rule (i.e. it ends with "5" or something) then you could programatically use LINQ to only populate your DataSource attribute with those values :

    // Determine which data to show
    var data = GetYourNumbersHere();
    
    if(ShouldFilter)
    {
        // This is a very basic example to demonstrate filtering
        data = data.Where(d => d.ToString().EndsWith("5"));
    }
    
    YourDropDown.DataSource = data;
    YourDropDown.DataBind();

    I would like to do that dynamically so that when new values are available in the source collection it works just the same way without any change to the system.

    If you are expecting your data source to change frequently and want to update the list based off of that, then you are likely going to need to rebind your list every time that your page is posted back :

    YourDropDown.DataSource = GetYourData();
    YourDropDown.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 21, 2017 8:57 PM

All replies

  • User2103319870 posted

    When data is bound to the ddl control, I want to hide or possibly ignore the items that are 1.25, 1.75 and so on. I do not want to hard code and I would like to do that dynamically so that when new values are available in the source collection it works just the same way without any change to the system.

    After binding the data you can use FindByText method to find particular list items in dropdownlist and then remove the value from dropdownlist

     protected void Page_Load(object sender, EventArgs e)
            {
    
                //Ensure that you are using this code after the binding dropdwonlist with data 
                ListItem removeListItem = DropDownList6.Items.FindByText("1.75");
                DropDownList6.Items.Remove(removeListItem);
    
                ListItem removeListItem1 = DropDownList6.Items.FindByText("2.75");
                DropDownList6.Items.Remove(removeListItem1);
            }

    Tuesday, February 21, 2017 8:22 PM
  • User1057158394 posted

    Hi,

    Thanks for your reply. I do not want to hard code the values. I want to know if there is a way to dynamically determine if it is a quarter value and then hide/remove it from the list at bind.

    Thanks.

    Tuesday, February 21, 2017 8:28 PM
  • User2103319870 posted

    I want to know if there is a way to dynamically determine if it is a quarter value and then hide/remove it from the list at bind.

    From above list what all values are expected to bind to dropdownlist if we remove the quarter values

    Tuesday, February 21, 2017 8:36 PM
  • User1057158394 posted

    Allowed values that can bind..It just that the quarters that should be ignored.

    1.00, 1.50, 2.00, 2.50, 3.00, 3.50, 4.00..and so on

    Tuesday, February 21, 2017 8:57 PM
  • User281315223 posted

    When data is bound to the ddl control, I want to hide or possibly ignore the items that are 1.25, 1.75 and so on.

    How are you planning on defining which items should and should not be hidden? If there is a specific rule (i.e. it ends with "5" or something) then you could programatically use LINQ to only populate your DataSource attribute with those values :

    // Determine which data to show
    var data = GetYourNumbersHere();
    
    if(ShouldFilter)
    {
        // This is a very basic example to demonstrate filtering
        data = data.Where(d => d.ToString().EndsWith("5"));
    }
    
    YourDropDown.DataSource = data;
    YourDropDown.DataBind();

    I would like to do that dynamically so that when new values are available in the source collection it works just the same way without any change to the system.

    If you are expecting your data source to change frequently and want to update the list based off of that, then you are likely going to need to rebind your list every time that your page is posted back :

    YourDropDown.DataSource = GetYourData();
    YourDropDown.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 21, 2017 8:57 PM
  • User1057158394 posted

    Thanks. The first one did the trick.

    Tuesday, February 21, 2017 9:26 PM