How to programmatically set SelectedValue of Dropdownlist when it is bound to XmlDataSource How to programmatically set SelectedValue of Dropdownlist when it is bound to XmlDataSource asp.net asp.net

How to programmatically set SelectedValue of Dropdownlist when it is bound to XmlDataSource


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;        }    }


DropDownList1.Items.FindByValue(stringValue).Selected = true; 

should work.


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"));            }        }