2011-10-08 52 views
1

我在我的項目中使用c#和asp.net。我想獲得dropdownlist的selectedindex,但我始終得到0.Here是我的代碼綁定下拉列表與數據獲取下拉列表的選擇索引

MySqlDataReader dr = null; 
     try 
     { 
      //////////////Opening the connection/////////////// 

      mycon.Open(); 
      string str = "select category from lk_category"; 
      MySqlCommand command = mycon.CreateCommand(); 
      command.CommandText = str; 
      dr = command.ExecuteReader(); 
      DropDownList1.DataSource = dr; 
      DropDownList1.DataValueField = "category"; 
      DropDownList1.DataBind(); 
      dr.Close(); 
      str = "select technology from lk_technology"; 
      command.CommandText = str; 
      dr = command.ExecuteReader(); 
      DropDownList2.DataSource = dr; 
      DropDownList2.DataValueField = "technology"; 
      DropDownList2.DataBind(); 
     } 
     catch (Exception ex) { Response.Write("Exception reding data" + ex); } 
     finally 
     { 
      //dr.Close(); 
      mycon.Close(); 
     } 

,我試圖通過獲取所選指數:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     catID = DropDownList1.SelectedIndex+1; 
    } 
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     techID = DropDownList2.SelectedIndex; 
    } 

這裏是我的Page_Load:

保護無效的Page_Load(對象發件人,EventArgs的) {

if (Session["valid"] == null) 
    Response.Redirect("admin.aspx"); 
panel1();///If session valid then show panel1; 

}

請告訴我在哪裏,我錯了。

+0

你可以發佈'Page_Load'方法代碼嗎? – Eranga

+0

@Eranga我在問題 –

回答

1

那是因爲你重新填充下拉頁面加載列表中沒有檢查它沒有後回來。

翹曲你的try-catch(下拉填充)代碼

if (!this.IsPostBack) 
{ 
    ... 
} 

應該解決的問題。

+0

中添加了page_load,這確實解決了這個問題。謝謝一噸... –

相關問題