必填字段验证器允许提交空文本框

时间:2018-04-10 02:02:09

标签: c# asp.net requiredfieldvalidator asp.net-validators

我有一个必填字段验证器,用于提交表单的两个文本框。我的表格代码如下。由于某些奇怪的原因,它不会验证并允许将空字符串输入数据库。输入的字段是char,但它不会将其提交为null,只是空白。

前端

    <td style="width: 315px; height: 22px;border-right: solid 1px #a32020;">
                    <asp:TextBox ID="txtCompanyName" runat="server" BorderColor="#A32020" style ="border-right: solid 1px #a32020;" OnTextChanged="txtCompanyName_TextChanged" CausesValidation="True"></asp:TextBox>
                &nbsp;<asp:RequiredFieldValidator ID="rfvCoName" runat="server" ControlToValidate="txtCompanyName" Display="Dynamic" ErrorMessage="Please do not leave the Company Name blank." ValidationGroup="vgCompanyCreate"></asp:RequiredFieldValidator>
                </td>

C#

    protected void btnSubmitCompany_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            // Creating a new connection to the database. 
            -- Insert Connection String --

            // Creating a new command to insert the input values into the database.
            SqlCommand SubmitCompanyCmd = new SqlCommand("Insert into Employer ([EmployerID], [Name], [Status], [Remarks], [DateLastModified], [LastModifiedBy]) Values(@inputCoID, @inputCoName, @inputCoStatus, @inputCoRemarks, @currentDateTime, 'Admin')", conn);

            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoID", txtCompanyID.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoName", txtCompanyName.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoStatus", ddlCompanyStatus.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoRemarks", txtCompanyRemarks.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@currentDateTime", DateTime.Now);

            conn.Open();
            SubmitCompanyCmd.ExecuteNonQuery();
            conn.Close();
            lblSuccessMsg.Text = "Yay. xqc";
        }

编辑:忽略“插入连接字符串”部分,实际连接字符串与其他函数一起测试时效果很好。

2 个答案:

答案 0 :(得分:1)

我设法找到了解决方案。

基本上我发现的是所有相关领域也需要验证组。不只是验证者。

答案 1 :(得分:0)

最好的方法是在代码后面添加代码(.cs类)

 protected void btnSubmitCompany_Click(object sender, EventArgs e)
    {
txtCompanyName.BorderColor = System.Drawing.Color.White;

if (txtCompanyName.Text="")
{
txtCompanyName.BorderColor = System.Drawing.Color.Red;
//Add error message if you want
}
else
{
if (Page.IsValid)
        {
            // Creating a new connection to the database. 
            -- Insert Connection String --

            // Creating a new command to insert the input values into the database.
            SqlCommand SubmitCompanyCmd = new SqlCommand("Insert into Employer ([EmployerID], [Name], [Status], [Remarks], [DateLastModified], [LastModifiedBy]) Values(@inputCoID, @inputCoName, @inputCoStatus, @inputCoRemarks, @currentDateTime, 'Admin')", conn);

            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoID", txtCompanyID.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoName", txtCompanyName.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoStatus", ddlCompanyStatus.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@inputCoRemarks", txtCompanyRemarks.Text);
            SubmitCompanyCmd.Parameters.AddWithValue("@currentDateTime", DateTime.Now);

            conn.Open();
            SubmitCompanyCmd.ExecuteNonQuery();
            conn.Close();
            lblSuccessMsg.Text = "Yay. xqc";
}
}

或者你可以使用非常基本的bootstrap“required”属性。

<asp:TextBox ID="txtCompanyName" runat="server" BorderColor="#A32020" style ="border-right: solid 1px #a32020;" OnTextChanged="txtCompanyName_TextChanged"  requrired="true"></asp:TextBox>