2012-04-12 112 views
3

我有一個DropDownList連接到ObjectDataSource。在我的頁面加載中,我根據情況將selectedvalue設置爲特定值。我怎樣才能在我的方法selectedindexchanged「重置」選定的值,所以你仍然可以選擇其他選項?或者我不應該使用selectedvalue來設置下拉列表的默認值?Dropdownlist重置SelectedValue

<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="ObjectDataSource" DataTextField="Type" DataValueField="TypeId" 
    AutoPostBack="true" onselectedindexchanged="DropDownList_SelectedIndexChanged" > 
      </asp:DropDownList> 

protected void Page_Load(object sender, EventArgs e) 
{ 
     int default = category.TypeId; 
     DropDownList.SelectedIndex = default; 

} 

protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) 
{ 

    int id = int.Parse(DropDownList.SelectedValue); 
    Session["id"] = id; 
} 
+0

你請了一些代碼,詳細點嗎? – sarwar026 2012-04-12 11:39:55

+0

編輯我的問題 – 2012-04-12 12:01:52

回答

1

你應該只DataBindDropDownList並設置其默認值if(!IsPostBack)

protected void Page_Load(Object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
    { 
     int default = category.TypeId; 
     DropDownList.SelectedValue = default.ToString(); 
    } 
} 

Page.IsPostBack Property

1

SelectedValue屬性設置爲空字符串:

DropDownList.SelectedValue = string.Empty; 
17

你可以使用ClearSelect離子法下拉列表中

DropDownList.ClearSelection(); 

感謝

迪普

相關問題