2011-10-24 66 views
1

我正在一個項目中,我有一個OdbcDataReader從查詢中讀入數據,並使用查詢中的項目填充DropDownList。當我運行的Web應用程序,並選擇在列表中選擇的值永遠不會改變的回發是否啓用與否不同的項目中,smartpaging啓用,或者如果將EnableViewStateASP.Net DropDownList SelectedIndex不變

protected void populateGrid(OdbcDataReader reader) 
    { 
     ClientDropDownList.DataSource = reader; 
     ClientDropDownList.DataTextField = "company"; 
     ClientDropDownList.DataBind(); 

    } 
+0

請提供一些更多的信息。 您使用哪個頁面生命週期階段來調用此** populateGrid(...)**方法?如果你想在後面的代碼中獲得選定的值,你用什麼方法來做到這一點? –

+0

您還應該設置[DataValueField](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.datavaluefield.aspx)。 –

回答

4

首先,您應該爲Dropdownlist控件設置AutoPostBack = true。

第二件事情是你應該檢查的IsPostBack

綁定在Page_Load事件中的值
If(! IsPostBack()) 
{ 

    Bind your dropdownlist here. 
} 

THRID的是作爲每Asp.Net生命週期process.Page負荷將火每次時間。當時有史以來頁面獲得更新。

1

檢查你是不是在每個調用PopulateGridPostBack。人們遇到的最常見問題之一。

如果您在您的Page_Load中致電PopulateGrid將其包裝在if (!IsPostBack)中。

0

您可以使用此代碼:

foreach (ListItem item in YourDropDownList.Items) 
    { 
      if (item.Text == defaultText) 
      { 
       item.Selected = true; 
       break; 
      } 
    } 
相關問題