2010-08-18 66 views
0

從下拉列表中選擇一個值時遇到問題。無論我選擇什麼,只有第一個值始終選擇。請幫助...從下拉列表中選擇一個值的問題

這裏是代碼...

protected void Button4_Click(object sender, EventArgs e) 
{ 
    SqlConnection con; 

    String strcon = ConfigurationManager.AppSettings["Constr"].ToString(); 
    try 
    { 
     if (!IsPostBack) 
     { 
     con = new SqlConnection(strcon); 
     con.Open(); 
     SqlDataAdapter da = new SqlDataAdapter("select user_name from user_details where role='USER'", con);  
     DataSet ds = new DataSet(); 

      da.Fill(ds); 
      DropDownList1.DataTextField = "user_name"; 
      DropDownList1.DataSource = ds.Tables[0].DefaultView; 
      DropDownList1.DataBind(); 
     } 

    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message.ToString()); 

    } 




protected void Button6_Click(object sender, EventArgs e) 
{ 

    string Name = Session["name"].ToString(); 
    SqlConnection con; 

    String strcon = ConfigurationManager.AppSettings["Constr"].ToString(); 
    try 
    { 
     con = new SqlConnection(strcon); 
     con.Open(); 


     SqlDataAdapter da = new SqlDataAdapter("select user_name,Arival,late,Day_count from attedance where user_name='" + DropDownList1.SelectedItem.Text + "' ", con); 
     DataSet ds = new DataSet(); 

     da.Fill(ds); 
     GridView1.DataSource = ds.Tables[0].DefaultView; 
     GridView1.DataBind(); 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message.ToString()); 

    } 
+1

這是運行在UpdatePanel或類似的東西嗎?否則,你需要在Page_Load – Tchami 2010-08-18 08:27:36

+0

上選擇一個項目,它用於從表 – Alivia 2010-08-18 08:34:25

+0

中獲取值,非常感謝.......它必須在頁面加載中給出我把它錯誤地放在按鈕控制中,它現在正在工作。 。:) – Alivia 2010-08-18 09:07:06

回答

1

不要完全得到你的代碼。你已經顯示了兩個按鈕點擊事件處理程序。

第一個填充下拉列表,因此第一個項目將被選中(這就是它的工作原理)。

第二個填充gridview。

如果通過點擊'Button4'(重命名按鈕來清楚它們的作用)是否出現問題,那麼這就是您的問題?

此外,你沒有關閉你的SqlConnection。使用使用塊:

using (SqlConnection connection = new SqlConnection(connectionString)) 
{ 
    //Do work here 
} 

編輯:啊,只是注意到(!IsPostBack)。

是否啓用ViewState的下拉菜單?

+0

我想(我希望)問題是點擊Button6時。即點擊Button4,然後用戶選擇一些內容並點擊Button6。儘管完全同意命名和使用()塊。 – DavidGouge 2010-08-18 08:37:40

+0

非常感謝.......它必須在頁面加載中給出我把它錯誤地放在按鈕control.It正在工作.. :) – Alivia 2010-08-18 09:06:32

相關問題