1

我正在开始编写网站代码,目前正在创建用户创建页面。所有验证工作正常,但我不确定将什么放入 checkUsername 方法以验证用户名不存在。下面是我的 ASP.NET 代码。

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  <p>
  <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="There were errors on the page:" />
  </p>
  <p>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  ControlToValidate="username" ErrorMessage="Username is required."> *</asp:RequiredFieldValidator>
  Enter Username <asp:TextBox ID="username" runat="server"></asp:TextBox>
     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="username" ErrorMessage="Username must be 4-10 letters or numbers." ValidationExpression="[a-zA-Z0-9]{4,10}" />
        <asp:CustomValidator ID="CustomValidator1" runat="server" controltovalidate="username" errormessage="Username is already in use." OnServerValidate="checkUsername" />
 </p>
 <p>
 Enter Password <asp:TextBox ID="entPWD" runat="server" TextMode="Password"></asp:TextBox>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" display="dynamic" ControlToValidate="entPWD" ErrorMessage="Password must contain one of the following: @#$%^&*/." ValidationExpression=".*[@#$%^&*/].*" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" display="dynamic" ControlToValidate="entPWD" ErrorMessage="Password must be 6-12 characters." ValidationExpression="[^\s]{6,12}" />
 </p>
 <p>
 Confirm Password <asp:TextBox ID="confPWD" runat="server" TextMode="Password"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="entPWD" ControlToCompare="confPWD" ErrorMessage="Passwords do not match." />
    </p>
    <asp:Button type="submit" ID="Button1" runat="server" Text="Submit" />
</asp:Content>

这是我的代码背后:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private bool checkUsername(string username)
    {
        string connString = "Data Source=serveraddress;Initial Catalog=database;User Id=username;Password=password;";
        string cmdText = "SELECT COUNT(*) FROM igs_users WHERE username = @username";

        using(SqlConnection conn = new SqlConnection(connString))
            {
            conn.Open();

            using(SqlCommand cmd = new SqlCommand(cmdText, conn))
                {
                cmd.Parameters.AddWithValue("@username", username);

                int count = (int)cmd.ExecuteScalar();

                return (count > 0);
                }
            }
    }
}

当我运行代码时,我收到以下错误消息:

Compiler Error Message: CS1061: 'ASP.default_aspx' does not contain a definition for 'checkUsername' and no extension method 'checkUsername' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)

不知道我哪里出错了。我有使用 System.Data.SqlClient; 在后面的代码中,还添加了对 system.data.dll 的引用。

任何帮助,将不胜感激!

4

2 回答 2

5

自定义验证方法需要以下方法签名:

void checkUsername(object source, ServerValidateEventArgs args)

ServerValidateEventArgs 不会将用户名作为字符串传递,而是具有该值。

而不是返回 true 或 false 进行验证,使用这个:

args.IsValid = true; 

有关使用 CustomValidators 的更多信息,请参阅此页面:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.onservervalidate (v=vs.80).aspx

于 2012-10-03T23:36:04.080 回答
1

尝试制作方法public而不是private. 您的 ASPX 页面正在尝试访问它,但它是private.

于 2012-10-03T23:36:14.910 回答