2015-10-15 71 views
0

我將數據綁定到下拉列表,並且我想要捕獲第一個元素以及第二個元素。但是,我試過的兩種方法只捕獲第一個元素?有人可以告訴我如何補救?
這是我如何綁定到我的下拉從下拉列表中訪問信息

protected void BindDropDown() 
{ 
    var itemandprice = new List<ListItem> 
    { 
    new ListItem("Flannel Pajamas", "9"), 
    new ListItem("Fleece Jacket", "30"), 
    new ListItem("Flannel Shirt", "40") 
    } 

    this.dropdownlist123.DataSource = itemandprice; 
    this.dropdownlist123.DataBind(); 
} 


選定的索引更改事件

protected void dropdownlist123_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    this.txtOne.Text = dropdownlist123.SelectedValue; 
    this.txtTwo.Text = dropdownlist123.SelectedItem.Text; 
} 


這兩個文本框在它具有值「法蘭絨睡衣」,而不是我之後會是法蘭絨睡衣和9

爲了捕獲這兩個元素,我需要做什麼?

編輯1
每@Alex Krups的建議,我嘗試這種語法,但是這給了我爲我的語法上面的輸出

this.txtOne.Text = dropdownlist123.SelectedItem.Value; 
this.txtTwo.Text = dropdownlist123.SelectedItem.Text; 

回答

1

您需要設置您的DropDownList的DataTextField和DataValueField。

請試試這個:

this.DropDownList1.DataTextField = "Text"; 
this.DropDownList1.DataValueField = "Value"; 
this.DropDownList1.DataSource = itemandprice; 
this.DropDownList1.DataBind(); 

希望這有助於〜

+0

就是這樣,謝謝! – user5382234

0

相同的嘗試

this.txtOne.Text = dropdownlist123.SelectedItem.Value; 
this.txtTwo.Text = dropdownlist123.SelectedItem.Text; 
+0

相同返回的結果,因爲我有。 – user5382234