一个提交按钮

时间:2012-09-05 21:39:19

标签: c# asp.net textbox submit

我有两个文本框和两个提交按钮。每个按钮都在文本框中提交信息,我希望有一个提交按钮,可以一次提交两个texbox。问题是,如果一个文本框为空,则它会用空数据覆盖数据。如果文本框中有内容,我希望它只提交信息。一些伪代码可能会有所帮助:

  • 如果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 个答案:

答案 0 :(得分:2)

看起来你只需要在尝试运行的每个块之前添加if(Push.Text!=“”)和if(Sits.Text!=“”)

答案 1 :(得分: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
 }
    }