如果c#文本框中存在用户名,请检查数据库

时间:2015-07-10 12:21:53

标签: c# button username

我打算根据文本框中的用户输入检查用户名,我打算在放入现有用户名后立即收到错误消息。现在我有这个代码。我没有遇到错误,但它无法正常工作。我在这里失踪了什么?

protected void btn_Registration_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            string insertQuery = "insert into UserData(Username,Firstname,Lastname,Email,Password,CustomerType,DeliveryAddress,Zip,ContactNumber)values(@Username,@Firstname,@Lastname,@Email,@Password,@CustomerType,@DeliveryAddress,@Zip,@ContactNumber)";
            SqlCommand scm = new SqlCommand(insertQuery, conn);
            scm.Parameters.AddWithValue("@Username", txtUser.Text);
            scm.Parameters.AddWithValue("@Firstname", txtFN.Text);
            scm.Parameters.AddWithValue("@Lastname", txtLN.Text);
            scm.Parameters.AddWithValue("@Email", txtEmail.Text);
            scm.Parameters.AddWithValue("@Password", BusinessLayer.ShoppingCart.CreateSHAHash(txtPW.Text));
            scm.Parameters.AddWithValue("@CustomerType", RadioButtonList1.SelectedItem.ToString());
            scm.Parameters.AddWithValue("@DeliveryAddress", txtAddress.Text);
            scm.Parameters.AddWithValue("@Zip", txtZip.Text);
            scm.Parameters.AddWithValue("@ContactNumber", txtContact.Text);

            scm.ExecuteNonQuery();
            Session["Contact"] = txtContact.Text;
            Session["Email"] = txtEmail.Text;
            Session["DeliveryAddress"] = txtAddress.Text;
            label_register_success.Text = ("Registration Successful!");
            //Response.Redirect("Home.aspx");
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Error:" + ex.ToString());
        }
    }

    protected void txtUser_TextChanged(object sender, EventArgs e)
    {
        if (Page.IsPostBack == true)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from UserDAta where Username='" + txtUser.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            Imagemessage.Visible = true;
            lblusercheck.Visible = true;
            Imagemessage.ImageUrl = "NotAvailable.jpg";
            lblusercheck.Text = "Username already exists.";
        }
        else
        {
            Imagemessage.Visible = true;
            lblusercheck.Visible = true;
            Imagemessage.ImageUrl = "Available.gif";
            lblusercheck.Text = "You can choose this username.";
        }
    }
    }  

1 个答案:

答案 0 :(得分:0)

任何单键按下都不会调用TextChange事件。你应该做的是使用javascript在客户端函数 keyup

中创建回服务器(例如使用__doPostBack)

此外,这将是一个回发,你不应该在Handler中检查!isPostBack。

更新

Markup:
<asp:TextBox runat="server" ID="txt" onkeyup="check(this);" />


Javascript
function check(txt) {

    __doPostBack('Control to Update', 'Filter--'+txt.value);
}

codebehind:
form_load:
parameter = Request["__EVENTARGUMENT"];
if (parameter != null && parameter.startsWith("Filter--"))
{
    //Do your check here.
}

我不喜欢这种编码,因为它太乱了。