2011-07-01 86 views
2

我需要設置值「Y」爲DropDownList控制dynamically.When我試圖通過了selectedValue它給了錯誤,如對象引用動態設定值下拉列表爲空.plz幫助如何在後面的C#代碼

回答

3

首先要確定Y在你的asp:DropDownList裏面。然後,請執行此操作

if (DropDownList1.Items.FindByValue("Y") != null) 
{ 
    DropDownList1.Items.FindByValue("Y").Selected = true; 
} 
0

SelectedValue僅在使用數據綁定時才起作用。如果通過Visual Studio中的Windows窗體設計器或通過操縱DropDownList.Items集合手動填寫列表中,您需要如下使用SelectedItem

DropDownList1.SelectedItem = "Y"; 
1

假設「Y」是你的下拉列表:

DropDownList1.SelectedValue = DropDownList1.Items.FindByValue(「Y」)。Tostring();

它的工作原理。

相關問題