Setting dropdownlist selecteditem programmatically

前端 未结 11 1692
北海茫月
北海茫月 2020-11-28 07:47

I want to set the selecteditem attribute for an ASP.Net dropdownlist control programmatically.

So I want to pass a value to the dropdownlist control to

相关标签:
11条回答
  • 2020-11-28 08:13

    Well if I understood correctly your question. The Solution for setting the value for a given dropdownlist will be:

    dropdownlist1.Text="Your Value";
    

    This will work only if the value is existing in the data-source of the dropdownlist.

    0 讨论(0)
  • 2020-11-28 08:14

    Here is the code I was looking for :

    DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByText("PassedValue"));
    

    Or

    DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByValue("PassedValue"));
    
    0 讨论(0)
  • 2020-11-28 08:19

    ddlData.SelectedIndex will contain the int value To select the specific value into DropDown :

    ddlData.SelectedIndex=ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));
    

    return type of ddlData.Items.IndexOf(ddlData.Items.FindByText("value")); is int.

    0 讨论(0)
  • Just Use this oneliner:

    divisions.Items.FindByText("Some Text").Selected = true;
    divisions.Items.FindByValue("some value").Selected = true;
    

    where divisions is a dropdownlist control.

    Hope it helps someone.

    0 讨论(0)
  • 2020-11-28 08:20
    var index = ctx.Items.FirstOrDefault(item => Equals(item.Value, Settings.Default.Format_Encoding));
    ctx.SelectedIndex = ctx.Items.IndexOf(index);
    

    OR

    foreach (var listItem in ctx.Items)
      listItem.Selected = Equals(listItem.Value as Encoding, Settings.Default.Format_Encoding);
    

    Should work.. especially when using extended RAD controls in which FindByText/Value doesn't even exist!

    0 讨论(0)
  • 2020-11-28 08:25

    If you need to select your list item based on an expression:

    foreach (ListItem listItem in list.Items)
    {
        listItem.Selected = listItem.Value.Contains("some value");
    }
    
    0 讨论(0)
提交回复
热议问题