我试图在现场基本检查输入的电子邮件是否已经注册。
试图做到这一点(我想在链接中做的一个例子,无法上传图片):
http://2.bp.blogspot.com/-2gcqeLxtuOs/UTxEge1d9XI/AAAAAAAAAz8/79M9RyYRHZs/s1600/UserAvailability.gif
我似乎不明白我的代码有什么问题,可能缺乏知识或忽视某些东西。我的代码:
Register.aspx:
function UserChecker(username) {
lbl = document.getElementById('<%=Label2.ClientID %>');
if (username == '') {
lbl.innerHTML = 'Please Enter a User Name';
lbl.style.color = "black";
}
else {
PageMethods.UserNameChecker(username, OnSucceeded);
}
}
function OnSucceeded(result, userContext, methodName)
{
lbl = document.getElementById('<%=Label2.ClientID %>');
if (methodName == "UserNameChecker")
{
if (result == true)
{
lbl.innerHTML = 'User name not available';
lbl.style.color = "red";
}
else
{
lbl.innerHTML = 'User name available';
lbl.style.color = "green";
}
}
}
<asp:Label ID="Label1" runat="server" Text="User Name"></asp:Label>
<br />
<asp:TextBox ID="email" runat="server" AutoPostBack="true" onkeyup="UserChecker(this.value)" style="font-size: 20px; text-align:center; border-radius: 20px;" class="RegisterButton" ValidationGroup="registergroup" placeholder="What's your Email?"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
Register.aspx.cs:
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.Web.Services;
public partial class CheckUserAvailability : System.Web.UI.Page
{
[WebMethod]
public static bool UserNameChecker(string UserName)
{
if (WebUser.IsUserNameExist(UserName) == true)
{
return true;
}
else
{
return false;
}
}
}
WebUser.cs(我创建的一个类):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
//Get the username if the entered user name is in the table.
public class WebUser
{
public static bool IsUserNameExist(string UserName)
{
OleDbConnection objConnUser = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + HttpContext.Current.Server.MapPath("~") + @"\App_Data\Database.accdb" + "");
objConnUser.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
OleDbParameter objParam = new OleDbParameter();
OleDbCommand objCmd = new OleDbCommand("CheckOnSpot", objConnUser);
objCmd.CommandType = CommandType.StoredProcedure;
objParam = new OleDbParameter("@UserName", OleDbType.Char);
objParam.Value = UserName;
objCmd.Parameters.Add(objParam);
if (objCmd.ExecuteScalar() != null)
{
return true;
}
else
return false;
}
}
UsersInfo.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
public class UsersInfo
{
public int UserID;
public string firstname;
public string email;
public string password;
public string lastname;
public int age;
public string phonenumber;
public string picture;
public UsersInfo()
{
}
}
CheckOnSpot查询(无论如何都需要它):
SELECT email
FROM users
WHERE email = [@UserName];
提前致谢。希望你能看到我错过的东西。
答案 0 :(得分:0)
ASPX ADD:OnTextChanged =“email_TextChanged”
<asp:TextBox ID="email" runat="server" AutoPostBack="true" onkeyup="UserChecker(this.value)" style="font-size: 20px; text-align:center; border-radius: 20px;" class="RegisterButton" ValidationGroup="registergroup" placeholder="What's your Email?" ></asp:TextBox>
背后的代码:
protected void email_TextChanged(object sender, EventArgs e)
{
string email= email.Text;
string conStr;
conStr = ConfigurationManager.ConnectionStrings["cnSting_Con"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
con.Open();
// WRITE STORED PROCEDURE TO CHECK EMAIl ID
SqlCommand cmd = new SqlCommand("CheckEmailExist ", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@email", email));
DataTable ds = new DataTable();
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(ds);
con.Close();
存储过程:编写程序[CheckEmailExist]