2012-07-20 15 views
0

我在我的asp.net網頁中有一個下拉列表。它是一個服務器端控件。我正在做一個ajax調用,並在ajax調用中,我將一個新項目添加到ddl並將其設置爲選定。它在頁面上顯示得很好。但是,當我回發所選項目時,會在下拉列表中顯示舊選定項目。DropDownList選擇不反映在服務器端

// ddlCaseNumber is the id of the dropdownlist and its clientid property is set 
// to static. 
// Removes the selected attribute of selected selected item . 
$('#ddlCaseNumber option:selected').removeAttr("selected"); 

// Add the new item to dropdownlist. 
$('#ddlCaseNumber').append('<option selected="selected" value=' + crmid + '>' 
          + crmid + '</option>'); 

// Code behind code to get the new value. 
// This line is giving the old value instead of giving new value. 
string strNewValue = ddlCaseNumber.SelectedItem.Value; 

任何人都可以請告訴我如何在代碼後面添加新項目嗎?

在此先感謝。

+1

請參閱http://stackoverflow.com/questions/1271172/options-added-to-select-by-javascript-lost-in-postback – 2012-07-20 08:18:22

回答

2

在客戶端上動態添加的項目將不在在服務器上自動複製回發。

一種選擇是以某種方式存儲的新項目已添加(在像一個<input type="hidden"><asp:HiddenField>),然後手動添加項目,作爲對後回您Init頁面的一部分。

另一種選擇是調用AJAX將信息存儲在服務器上,如Session變量,然後再手動添加它在回發。