0

我有一些问题:下面的代码用新行更新了表格,但是这一行中的所有字段都是空的。我想将文本字段中的输入数据放入成员表中的新行中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Default4 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtAdress.Text = "";
        txtResidence.Text = "";
        txtEmail.Text = "";
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(
            WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES (@txtFirstName, @txtLastName, @txtAdress, @txtResidence, @txtEmail)", con);
        cmd.Parameters.AddWithValue("@txtFirstName", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@txtLastName", txtLastName.Text);
        cmd.Parameters.AddWithValue("@txtAdress", txtAdress.Text);
        cmd.Parameters.AddWithValue("@txtResidence", txtResidence.Text);
        cmd.Parameters.AddWithValue("@txtEmail", txtEmail.Text);

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();

            lblConfirm.Text = "Database updated, insert confirmed!";

            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtAdress.Text = "";
            txtResidence.Text = "";
            txtEmail.Text = "";
        }
        finally
        {
            con.Close();
        }
    }
}

如果我改变这一行:

SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES (@txtFirstName, @txtLastName, @txtAdress, @txtResidence, @txtEmail)", con);

对于这样的事情:

SqlCommand cmd = new SqlCommand("INSERT INTO Members(FirstName, LastName, Adress, Residence, Email) VALUES ('@txtFirstName', '@txtLastName', @txtAdress, @txtResidence, @txtEmail)", con);

它会打印出一行这样的字段:| @txt名字 | @txt姓氏| [空] | [空] | [空] |

任何帮助都会很棒,谢谢!

4

2 回答 2

1

每次按下注册按钮时,都会Page_Load执行,清空文本框,然后btnRegister_Click执行。此时您的文本框是空白的。

您需要在 Page_Load 事件中添加一个检查

   if(!IsPostBack)
   {
      ... // code to empty the textboxes
   }

MSDN 上的 Page.IsPostBack 属性

于 2013-02-05T10:35:22.397 回答
0

这里的问题....

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();

        lblConfirm.Text = "Database updated, insert confirmed!";

        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtAdress.Text = "";
        txtResidence.Text = "";
        txtEmail.Text = "";
    }

^^^^^^ 重置这里的所有字段不会有任何区别。它会在每次回发后发生。

试试这个

if(!IsPostBack)
   {
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtAdress.Text = "";
            txtResidence.Text = "";
            txtEmail.Text = "";
   }

因此,用户输入不会在按钮单击时重置为空。

这里有更好的方法来实现你的目标......

于 2013-02-05T10:46:38.567 回答