尝试进行验证可防止无效输入数据库,无法正常工作

时间:2014-11-05 20:07:00

标签: c# asp.net validation ms-access

我尝试使用输入到Microsoft Access数据库的fields Name, Student ID, Year of Birth, Gender, Email Address and Password创建用户注册表单。有适当的验证,以便如果密码长度少于8个字符,用户无法继续进行,除非它被更改。

但它没有用。验证消息不会出现,除了在远离文本字段标签后出现的电子邮件地址,并且尽管无效数据仍然可以保存到数据库中。我无法弄清楚原因。

这是我的HTML代码:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h1 class="auto-style7">
        <strong>REGISTER</strong></h1>
    <p class="auto-style2">
        <strong><span class="auto-style3">
        <asp:Label ID="Label1" runat="server" Text="Name:"></asp:Label>
        </span></strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style5"></asp:TextBox>
        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please insert name" ForeColor="#CC3300" ValidationGroup="MemberGroup"></asp:RequiredFieldValidator>
        </strong></p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label2" runat="server" Text="Student ID:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox2" runat="server" CssClass="auto-style5"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Student ID must start with 'TP' followed by six digits." ForeColor="#CC3300" OnServerValidate="CustomValidator2_ServerValidate" ControlToValidate="TextBox2" ValidationGroup="MemberGroup" ValidateEmptyText="True"></asp:CustomValidator>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label3" runat="server" Text="Year of Birth:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:DropDownList ID="DropDownList2" runat="server" CssClass="auto-style5">
        </asp:DropDownList>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label4" runat="server" Text="Gender:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="auto-style5">
            <asp:ListItem Value="0"></asp:ListItem>
            <asp:ListItem>Female</asp:ListItem>
            <asp:ListItem>Male</asp:ListItem>
        </asp:DropDownList>
        <asp:CustomValidator ID="CustomValidator3" runat="server" ControlToValidate="DropDownList1" ErrorMessage="Please select a gender." ForeColor="#CC3300" OnServerValidate="CustomValidator3_ServerValidate" ValidationGroup="MemberGroup" ValidateEmptyText="True"></asp:CustomValidator>
        </strong>
    </p>
<p class="auto-style2">
        <strong>
        <asp:Label ID="Label5" runat="server" Text="Email address:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox4" runat="server" CssClass="auto-style5"></asp:TextBox>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Valid email address required." ForeColor="#CC3300" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="TextBox4" ValidationGroup="MemberGroup"></asp:RegularExpressionValidator>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label6" runat="server" Text="Password:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox5" TextMode="Password" runat="server" CssClass="auto-style5"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Password must be at least 8 characters long." ForeColor="#CC3300" OnServerValidate="CustomValidator1_ServerValidate" ControlToValidate="TextBox5" ValidationGroup="MemberGroup" ValidateEmptyText="True"></asp:CustomValidator>
        </strong>
    </p>
    <p class="auto-style2">
        <strong>
        <asp:Label ID="Label7" runat="server" Text="Confirm password:" CssClass="auto-style3"></asp:Label>
        </strong>
    </p>
    <p>
        <strong>
        <asp:TextBox ID="TextBox6" TextMode="Password" runat="server" CssClass="auto-style5"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox5" ControlToValidate="TextBox6" ErrorMessage="Confirm Password must match Password." ForeColor="#CC3300" ValidationGroup="MemberGroup"></asp:CompareValidator>
        </strong>
    </p>
<p class="auto-style3">
        &nbsp;</p>
<p>
        <strong>
        <asp:Button ID="Button1" runat="server" CssClass="auto-style3" OnClick="Button1_Click" Text="Register" />
        </strong>
    </p>
    <p class="auto-style3">
        &nbsp;</p>
</asp:Content>

这是我的C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace APUBadmintonSportsClubWebsite
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //append item to the collection
        for (int i = 1900; i <= 2007; i++)
            this.DropDownList2.Items.Add(i.ToString());
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //formulate a string containing the details of 
        //the database connection
        string connectionString = "provider=Microsoft.Ace.OleDB.12.0;"
            + "data source="
            + Page.Server.MapPath("App_Data\\ABC.accdb");

        //create a OIeDbConnection object to connect to the database
        System.Data.OleDb.OleDbConnection conn = new
        System.Data.OleDb.OleDbConnection(connectionString);

        //create a OleDbCommand object
        System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand();

        // add a collection of parameters associated with a OIeDbCommand

        cmd.Parameters.Add("StudentID", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["StudentID"].Value = this.TextBox2.Text;

        cmd.Parameters.Add("StudentName", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["StudentName"].Value = this.TextBox1.Text;

        cmd.Parameters.Add("YearBorn", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["YearBorn"].Value = this.DropDownList2.Text;

        cmd.Parameters.Add("Gender", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["Gender"].Value = this.DropDownList1.Text;

        cmd.Parameters.Add("StudentMail", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["StudentMail"].Value = this.TextBox4.Text;

        cmd.Parameters.Add("Password", System.Data.OleDb.OleDbType.VarChar);
        cmd.Parameters["Password"].Value = this.TextBox6.Text;

        // set the commandText property of the OleDbCommand object to
        // the INSERT statement
        cmd.CommandText = "INSERT INTO [Members] ([StudentID],[StudentName],[YearBorn],[Gender],[StudentMail],[Password]) VALUES (@StudentID,@StudentName,@YearBorn,@Gender,@StudentMail,@Password)";


        //open the database connection using the open() method of the OleDbConnection object
        conn.Open();

        //call the ExcuteNonQuery() method of the OleDbCommand object 
        //to run the INSERT statement
        int numberOfRows = cmd.ExecuteNonQuery(); //exception here when inserting duplicate data

        //close the OleDbConnection object using using the Close () method 
        conn.Close();

        if (Page.IsValid)
        {

        }

        //redirect to View.aspx
        Response.Redirect("Home.aspx");
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length < 8)
            args.IsValid = false;
        else
            args.IsValid = true;
    }

    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length < 8)
            args.IsValid = false;
        else
            args.IsValid = true;
    }

    protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (DropDownList1.SelectedValue == "0") //want to select text of drop down selection
            args.IsValid = false;
        else
            args.IsValid = true;
    }

}
}

感谢任何帮助,如果需要,我会填写更多详细信息。

1 个答案:

答案 0 :(得分:2)

您需要在asp:Button控件上添加ValidationGroup =“MemberGroup”。或者您可以从所有其他验证器中删除ValidationGroup属性。您还需要在if(Page.IsValid)检查中的Click事件中包装代码。