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

前端 未结 4 868
时光取名叫无心
时光取名叫无心 2020-12-06 04:13

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

Now I want to set the SelectedValue of the drop do

相关标签:
4条回答
  • 2020-12-06 04:56

    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;
            }
        }
    
    0 讨论(0)
  • 2020-12-06 04:56

    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"));
                }
            }
    
    0 讨论(0)
  • 2020-12-06 04:58
    DropDownList1.Items.FindByValue(stringValue).Selected = true; 
    

    should work.

    0 讨论(0)
  • 2020-12-06 05:09

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

    0 讨论(0)
提交回复
热议问题