2012-09-05 242 views
-4

我有兩個文本框和兩個提交按鈕。每個按鈕提交的信息在文本框中,我想有1個提交按鈕,一次提交兩個texboxes沒有。問題是,如果一個文本框爲空,那麼它會用空數據覆蓋數據。如果文本框中有某些內容,我希望它只提交信息。一些僞代碼可能會有所幫助:單提交按鈕

  • 如果textbox1爲空,則不執行任何操作,如果textbox2有數據,則更新數據庫。
  • 如果textbox1有數據,則更新如果textbox2爲空則不更新textbox2。

這是我的代碼,我希望我的意思是說得通的。

protected void Button3_Click(object sender, EventArgs e) 
    { 

      SqlConnection conns = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDBConnectionString1"].ConnectionString); 
      SqlCommand cmd = new SqlCommand("UPDATE test SET SitUps = @SitUps WHERE (Id = @Id)", conns); 
      cmd.CommandType = CommandType.Text; 
      Label IdL = ((Label)FormView1.FindControl("IdLabel")); 
      cmd.Parameters.AddWithValue("@Id", IdL.Text); 
      cmd.Parameters.AddWithValue("@SitUps", Sits.Text); 
      conns.Open(); 
      cmd.ExecuteNonQuery(); 
      Label17.Text = "Successfully Submitted Sit-Ups!"; 

    } 

    protected void Button4_Click(object sender, EventArgs e) 
    { 

      SqlConnection conns = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDBConnectionString1"].ConnectionString); 
      SqlCommand cmd = new SqlCommand("UPDATE test SET pushUps = @pushUps WHERE (Id = @Id)", conns); 
      cmd.CommandType = CommandType.Text; 
      Label IdL = ((Label)FormView1.FindControl("IdLabel")); 
      cmd.Parameters.AddWithValue("@Id", IdL.Text); 
      cmd.Parameters.AddWithValue("@pushUps", Push.Text); 
      conns.Open(); 
      cmd.ExecuteNonQuery(); 
      Label18.Text = "Successfully Submitted Push-Ups!"; 

    } 

回答

2

看起來像你只需要如果添加(Push.Text!=「」),如果(Sits.Text!=「」)之前,每次嘗試運行塊

2

替換它與此:

protected void Button_Click(object sender, EventArgs e) 
    { 
//check Sits and Push empty or not 
if(Sits.Text!="") 
{ 
//update Sits here 
} 

if(Push.Text!="") 
{ 
//update Push here 
} 
    }