2013-10-28 67 views
0

我有DROPDOWNLIST我填充在Page_Load中在下拉Selected_Indexchanged事件的SelectedValue總是越來越復位的第一個值

的時間
protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       DataTable table = new DataTable(); 


       string connectionString = GetConnectionString(); 
       string sqlQuery = "select distinct sname from contacts where sname is not null"; 


       using (OracleConnection conn = new OracleConnection(connectionString)) 
       { 

        try 
        { 
         conn.Open(); 

         using (OracleCommand cmd = new OracleCommand(sqlQuery, conn)) 
         { 

          using (OracleDataAdapter ODA = new OracleDataAdapter(cmd)) 
          { 

           ODA.Fill(table); 

          } 

         } 
        } 
        catch (Exception ex) 
        { 
         Response.Write("Not Connected" + ex.ToString()); 
        } 

       } 

       //DropDownList1.DataSource = table; 
       //DropDownList1.DataValueField = ""; 

       DropDownList2.DataSource = table; 
       DropDownList2.DataValueField = "sname"; 
       DropDownList2.DataTextField = "sname"; 



       DropDownList2.DataBind(); 
      } 
} 

現在我要當DropDownList的項目改變

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      DataTable table = new DataTable(); 


      string connectionString = GetConnectionString(); 
      string val = DropDownList2.SelectedValue; 

      string sqlQuery = "SELECT distinct DUTY_DATE FROM DUTY_ROTA,DUTY_TYPES,CONTACTS WHERE DUTY_DATE between SYSDATE and SYSDATE+30 AND DUTY_ROTA.DUTY_TYPE = DUTY_TYPES.DUTY_TYPE AND SNAME IS NOT NULL and contacts.sname = '" + val + "' ORDER BY DUTY_DATE"; 
      using (OracleConnection conn = new OracleConnection(connectionString)) 
      { 

       try 
       { 
        conn.Open(); 

        using (OracleCommand cmd = new OracleCommand(sqlQuery, conn)) 
        { 

         using (OracleDataAdapter ODA = new OracleDataAdapter(cmd)) 
         { 

          ODA.Fill(table); 

         } 

        } 
       } 
       catch (Exception ex) 
       { 
        Response.Write("Not Connected" + ex.ToString()); 
       } 

      } 

      GridView1.DataSource = table; 
      GridView1.DataBind(); 

     } 
來填充一個GridView

我已啓用AutoPostBack。現在,當我將DropDownList項目更改爲不同的頁面時,頁面正在加載但始終保留第一個值。我試圖調試,我發現

string val = DropDownList2.SelectedValue; 

val變量總是查詢返回的第一個值。任何人都可以告訴我,我怎麼能擺脫這一點。我想在我選擇下拉列表中的任何項目時填充GridView。

+0

看看是否改爲DropDownList2.SelectedItem.Value有助於?即字符串val = DropDownList2..SelectedItem.Value; – Ratna

+0

它是一樣的。 :(我是否錯了什麼地方非常基礎? – Subhamoy

+0

你試過DropDownList2.SelectedItem.Text ?? – Rafay

回答

0

嗨,我認爲當你選擇其他項目時,dropdownlist再次收費。在頁面加載中放置一個斷點,看看你的下拉列表再次收費,我沒有看到其他原因。好運

+0

謝謝 - 我試圖調試 - DropDown總是填充查詢返回的第一個結果,不管我是什麼選擇.. – Subhamoy

0

您的下落downlist看起來應該如下:

<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> 
     </asp:DropDownList> 

確保的AutoPostBack被設置爲True。

您可以嘗試的另一件事是當索引大於-1時選擇該項目。

 if(DropDownList2.SelectedIndex != -1) 
    { 
     string val = DropDownList2.SelectedItem.Value; 
// enter code here 
    }