2013-03-22 49 views
3

編輯文本框我有下面的代碼在Page_Load中不能在asp.net

using (SqlConnection conn = new SqlConnection("myconnectionString")) 
     { 
      conn.Open(); 
      using (SqlCommand cmmnd = new SqlCommand("", conn)) 
      { 



       cmmnd.CommandText = "SELECT * FROM addsetting;"; 


       SqlDataReader rdr = cmmnd.ExecuteReader(); 
       while (rdr.Read()) 
       { 
        count++; 
        param = Convert.ToString(rdr["rowno"]); 
        TextBox1.Text = Convert.ToString(rdr["tostudent"]); 
        TextBox2.Text = Convert.ToString(rdr["tofaculty"]); 
        TextBox3.Text = Convert.ToString(rdr["studentday"]); 
        TextBox4.Text = Convert.ToString(rdr["facultyday"]); 
        TextBox5.Text = Convert.ToString(rdr["firstweek"]); 
        TextBox6.Text = Convert.ToString(rdr["secondweek"]); 
        TextBox7.Text = Convert.ToString(rdr["thirdweek"]); 

       } 
       rdr.Close(); 
      } 
      conn.Close();} 

運行,現在這個表單填寫後,我改變了價值觀,有一個「保存」按鈕。

stra上Button2_Click,當我嘗試訪問從文本框中編輯的值,我沒有得到新的值。該文本框具有舊值。

爲什麼文本框沒有編輯的值。

下面是我用來訪問值的代碼。

using (SqlConnection conn = new SqlConnection("myconnectionstring")) 
      { 
       conn.Open(); 



       using (SqlCommand cmd = new SqlCommand("", conn)) 
       { 
        cmd.Parameters.Add("@rowno", SqlDbType.VarChar).Value = param; 

        cmd.Parameters.AddWithValue("@tostudent", TextBox1.Text); 
        cmd.Parameters.AddWithValue("@tofaculty", TextBox2.Text); 
        cmd.Parameters.AddWithValue("@studentday", TextBox3.Text); 
        cmd.Parameters.AddWithValue("@facultyday", TextBox4.Text); 
        cmd.Parameters.AddWithValue("@firstweek", TextBox5.Text); 
        cmd.Parameters.AddWithValue("@secondweek", TextBox6.Text); 
        cmd.Parameters.AddWithValue("@thirdweek", TextBox7.Text); 


        cmd.CommandText = "UPDATE addsetting SET [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected] WHERE [email protected]"; 
        cmd.ExecuteNonQuery(); 

       } 

       conn.Close(); 

回答

5

您是否正在檢查您的page_load中的IsPostBack屬性?或者,您所概述的代碼是否運行,無論是否存在回發?

如果代碼的運行與回發無關,那麼在Button2_Click有機會訪問它們之前,您正在做的是覆蓋TextBox值。這是因爲在事件處理之前運行加載的page lifecycle

你可能只是想數據庫檢索在Page_Load中運行,如果它不是一個回發(即用戶沒有點擊提交按鈕),例如:

if (!Page.IsPostBack) 
{ 
    using (SqlConnection conn = new SqlConnection("myconnectionString")) 
    { 
     conn.Open(); 
     using (SqlCommand cmmnd = new SqlCommand("", conn)) 
     { 
      cmmnd.CommandText = "SELECT * FROM addsetting;"; 
      SqlDataReader rdr = cmmnd.ExecuteReader(); 

      while (rdr.Read()) 
      { 
       count++; 
       param = Convert.ToString(rdr["rowno"]); 
       TextBox1.Text = Convert.ToString(rdr["tostudent"]); 
       TextBox2.Text = Convert.ToString(rdr["tofaculty"]); 
       TextBox3.Text = Convert.ToString(rdr["studentday"]); 
       TextBox4.Text = Convert.ToString(rdr["facultyday"]); 
       TextBox5.Text = Convert.ToString(rdr["firstweek"]); 
       TextBox6.Text = Convert.ToString(rdr["secondweek"]); 
       TextBox7.Text = Convert.ToString(rdr["thirdweek"]); 
      } 

      rdr.Close(); 
     } 
    } 
} 
+1

+1瞭解頁面LifeCycle事件是ASP.Net編程中最困難的部分之一。抽象幾乎*太好*。 – mellamokb 2013-03-22 02:36:25

+0

感謝您的快速幫助,它解決了我的問題。我是c#和asp.net的新手,你的回答有助於獲得更多關於asp.net的知識 – 2013-03-22 02:59:40

0

這是所有關於page life cycle。 Page_Load在Click事件處理之前觸發。因此,當您單擊Button2時,Page_Load將運行並使用舊值重新填充表單,然後執行Button2.Click並將當前(現在的舊)值保存回數據庫。

1
if(!IsPostBack) 
{ 
    using (SqlConnection conn = new SqlConnection("myconnectionString")) 
    { 
     conn.Open(); 
     using (SqlCommand cmmnd = new SqlCommand("", conn)) 
     { 



      cmmnd.CommandText = "SELECT * FROM addsetting;"; 


      SqlDataReader rdr = cmmnd.ExecuteReader(); 
      while (rdr.Read()) 
      { 
       count++; 
       param = Convert.ToString(rdr["rowno"]); 
       TextBox1.Text = Convert.ToString(rdr["tostudent"]); 
       TextBox2.Text = Convert.ToString(rdr["tofaculty"]); 
       TextBox3.Text = Convert.ToString(rdr["studentday"]); 
       TextBox4.Text = Convert.ToString(rdr["facultyday"]); 
       TextBox5.Text = Convert.ToString(rdr["firstweek"]); 
       TextBox6.Text = Convert.ToString(rdr["secondweek"]); 
       TextBox7.Text = Convert.ToString(rdr["thirdweek"]); 

      } 
      rdr.Close(); 
     } 
     conn.Close();} 

}