1

我正在尝试验证三个文本框,它们都只是文本字段。它们都使用 sqlserver 连接到数据库。

现在我想知道当表单仍然为空时如何阻止表单将空记录添加到我的数据库中。我有一些 Javascript 验证,它告诉用户该字段为空,但是在您单击消息框的确定后,页面刷新并添加了一条空记录。

如果有人可以帮助我编写一些代码,那就太好了。

我的 javascript 看起来像这样:

    function myAddFunction() {
        var a = document.forms["form1"]["txtFirstName"].value;
        var b = document.forms["form1"]["txtLastName"].value;
        var c = document.forms["form1"]["txtvercode"].value;
        if (a == null || a == "") {
            alert("Please Enter a value");
            return false;
        }
        if (b == null || b == "") {
            alert("Please Enter a value");
            return false;
        }
        if (c == null || c == "") {
            alert("Please Enter a value");
            return false;
        }
        else {
            alert("The Member has been added!");
            return true;
        }
    }

我的 Default.aspx.cs 文件如下所示:

protected void btnAdd_Click(object sender, EventArgs e)
{

    SqlConnection conn = new SqlConnection(
    @"Data Source=(LocalDB)\v11.0; AttachDbFilename='|DataDirectory|\employees.mdf';
    Integrated Security=True");

    conn.Open();

    SqlCommand cmd = new SqlCommand("INSERT INTO membership (FirstName, LastName, MembershipClass, VerificationCode) VALUES ('" + txtFirstName.Text + "', '" + txtLastName.Text + "', '" + dropmemberclass.Text + "', '" + txtvercode.Text + "')", conn);

    cmd.ExecuteNonQuery();

    txtFirstName.Text = "";
    txtLastName.Text = "";
    txtvercode.Text = "";
    txtMemID.Text = "";
    txtmemdelete.Text = "";
    lblMemID.Text = "";
    lblMemIDSearch.Text = "";
}
4

4 回答 4

1

您可以为每个添加一个RequiredFieldValidatorTextBox

<asp:RequiredFieldValidator id="RequiredFieldValidator2"
                 ControlToValidate="txtFirstName"
                 Text="First name is a required field"
                 ForeColor="red" 
                 runat=server>

这会验证客户端和服务器端的数据,因此您不需要任何 Javascript 代码。

于 2013-05-09T02:23:58.177 回答
0

用这个替换代码:

SqlConnection conn = new SqlConnection(
    if(txtFirstName.Text != "" && txtLastName.Text != "" && txtvercode.Text = "")//And so on...
    {
        @"Data Source=(LocalDB)\v11.0; AttachDbFilename='|DataDirectory|\employees.mdf';
        Integrated Security=True");

        conn.Open();

        SqlCommand cmd = new SqlCommand("INSERT INTO membership (FirstName, LastName, MembershipClass, VerificationCode) VALUES ('" + txtFirstName.Text + "', '" + txtLastName.Text + "', '" + dropmemberclass.Text + "', '" + txtvercode.Text + "')", conn);

        cmd.ExecuteNonQuery();

        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtvercode.Text = "";
        txtMemID.Text = "";
        txtmemdelete.Text = "";
        lblMemID.Text = "";
        lblMemIDSearch.Text = "";
    }
}

在我添加的条件中添加更多...

于 2013-05-09T02:14:37.207 回答
0

您可以将任何键(如主键)添加到列中,这样它就不会接受空值并将 requiredfieldvalidator 添加到您的文本框中

于 2013-05-09T07:56:13.643 回答
0

检查这个

<asp:CustomValidator ID="myCustValidator" runat="server" ControlToValidate="txtFirstName"  OnServerValidate="ServerFirstNameValid"></asp:CustomValidator>

protected void ServerFirstNameValid(object source, ServerValidateEventArgs args)
{
   args.IsValid = !String.IsNullOrEmpty(txtFirstName.Text.Trim());
   if (!args.IsValid)
         myCustValidator.ErrorMessage="First Name should not be Empty";
}
于 2013-05-09T10:41:32.023 回答