28

I'm using XmlDataSource as the datasource for a dropdownlist.

Now I want to set the SelectedValue of the drop down when the page initially loads. I have tried the OnDataBound event of the drop down in which I could see the total items. But setting the SelectedValue didn't work. InOnDataBinding event, I couldn't even see the total items probably because the list isn't bound yet?

How can I set the selected index based on a value?

Cœur
  • 37,241
  • 25
  • 195
  • 267
MNIK
  • 1,581
  • 3
  • 16
  • 22
  • Are you using declarative or programmatic databinding? More specifically, is your dropdown list already populated at the point where you want to set the SelectedValue? – Jonathan Nesbitt Feb 25 '11 at 19:31
  • No, the dropdownlist was not populated because I want to set the SelectedValue in the Page_Load. – MNIK Feb 25 '11 at 19:48
  • 1
    Have you tried, after calling DataBind on your DropDownList, to do something like ddl.SelectedIndex = 0 ? – TheGeekYouNeed Feb 25 '11 at 19:55
  • 1
    One solution is to programmatically databind the dropdown list before you set the selected value. You might also find an event in the ASP.NET page life cycle that occurs after declarative databinding happens. – Jonathan Nesbitt Feb 25 '11 at 19:55
  • That worked. I was trying binding events, but never tried to call DataBind directly on the dropdownlist. But when I did, it worked. Thank you. – MNIK Feb 25 '11 at 20:00
  • Posting my comment as an answer. – TheGeekYouNeed Feb 25 '11 at 22:05
  • I just set the SelectedIndex in page load, no need to data bind. – Aaron Anodide Jun 09 '11 at 17:02
  • Possible duplicate of [Setting dropdownlist selecteditem programmatically](http://stackoverflow.com/questions/3496456/setting-dropdownlist-selecteditem-programmatically) – Michael Freidgeim Jan 16 '17 at 06:56

4 Answers4

79

This seems to work for me.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DropDownList1.DataBind(); // get the data into the list you can set it
            DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;
        }
    }
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
18
DropDownList1.Items.FindByValue(stringValue).Selected = true; 

should work.

SharpC
  • 6,974
  • 4
  • 45
  • 40
dpchimmili
  • 221
  • 2
  • 4
11

This is working code

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            { 
                    DropDownList1.DataTextField = "user_name";
                    DropDownList1.DataValueField = "user_id";
                    DropDownList1.DataSource = getData();// get the data into the list you can set it
                    DropDownList1.DataBind();

    DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("your default selected text"));
            }
        }
Atif Mahmood
  • 8,882
  • 2
  • 41
  • 44
-4

Have you tried, after calling DataBind on your DropDownList, to do something like ddl.SelectedIndex = 0 ?

TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43
  • 2
    Consider formatting this answer- besides it is wrong(or not entirely what the OP asked about). Just remove it. – Piotr Kula Dec 05 '12 at 15:26