Textbox TextChanged正在激活但不会改变 [英] Textbox TextChanged is Firing but not changing

查看:60
本文介绍了Textbox TextChanged正在激活但不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextBox TextChanged触发但当我输入不同的电子邮件地址和标签或点击下一个文本框来触发更改时没有任何反应。我再次运行程序,没有任何反应。为什么会这样?我做错什么了吗?我甚至刷新了页面并输入了不同的电子邮件地址,同样的事情发生了。有人可以帮我吗?



I have a TextBox TextChanged firing but when I enter a different Email Address and tab or click to the next textbox to fire the change nothing happens. I ran the program again and nothing happens. Why is this happening? Did I do something wrong? I even refreshed the page and entered in a different Email Address and the samething happened. Can someone help me with this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Security.Cryptography;

public partial class SubmitPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "Select count(*) from TableSecurity where EmailAddress='" + TextBoxEA.Text + "'";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select INST_ID, EmailAddress from TableSecurity", con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 1)
            {
                lblMessage.Text = "User Name Already Exist!!!";
            }
        }
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();
        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from TableCEO where EmailAddress='" + TextBoxEA.Text + "'";
        string cmdStr2 = "Select INST_ID, accessLevel, EmailAddress from TableIALO where EmailAddress='" + TextBoxEA.Text + "'";
        string insCmd = "Insert into TableSecurity (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        string insCmd2 = "Insert into TableSecurity (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        SqlCommand insertUser2 = new SqlCommand(insCmd2, con);
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);
        
        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            lblMessage.Text = "User Already Exist";
        }
        finally
        {
        }
    }

    protected void TextBoxEA_TextChanged(object sender, EventArgs e)
    {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con.Open();
            
            SqlCommand scmd = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from TableCEO where EmailAddress = '" + TextBoxEA.Text + "'", con);
            SqlCommand scmd2 = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from TableIALO where EmailAddress = '" + TextBoxEA.Text + "'", con);
            SqlDataReader dr = scmd.ExecuteReader();
            SqlDataReader dr2 = scmd.ExecuteReader();

            if (dr.Read())
            if (dr2.Read())
            
                {
                    TextBoxINST_ID.Text = dr["INST_ID"].ToString();
                    TextBoxaccessLevel.Text = dr["accessLevel"].ToString();
                    TextBoxINST_ID.Text = dr2["INST_ID"].ToString();
                    TextBoxaccessLevel.Text = dr2["accessLevel"].ToString();
                    
                }
            dr.Close();
            dr2.Close();
            con.Close();
        }
    }





它会为第一个连接而不是第二个连接触发。为什么会这样?



It fires for the first connection but not the second connection. Why is that?

推荐答案

在textchanged事件中,您使用scmd而不是scmd2作为dr2 SqlDataReader。所以你的第一个Select语句被调用了两次。









我改变了你的代码,尝试这样的事情:

In your textchanged event, you are using "scmd" instead of scmd2 for your dr2 SqlDataReader. So your first Select statement is getting called twice.




I altered your code, try something like this:
protected void TextBoxEA_TextChanged(object sender, EventArgs e)
  {
          using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString))
          {
              con.Open();

          SqlCommand scmd = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from TableCEO where EmailAddress = @TextBoxEA", con);
          SqlCommand scmd2 = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from TableIALO where EmailAddress = @TextBoxEA", con);

          scmd.Parameters.Add(new SqlParameter("@TextBoxEA", TextBoxEA.Text));
          scmd2.Parameters.Add(new SqlParameter("@TextBoxEA", TextBoxEA.Text));

          using (SqlDataReader dr = scmd.ExecuteReader())
          {
              while(dr.Read())
              {
                 TextBoxINST_ID.Text = dr["INST_ID"].ToString();
                 TextBoxaccessLevel.Text = dr["accessLevel"].ToString();
              }
          }

          using (SqlDataReader dr2 = scmd2.ExecuteReader())
          {
              while(dr2.Read())
              {
                 TextBoxINST_ID.Text = dr2["INST_ID"].ToString();
                 TextBoxaccessLevel.Text = dr2["accessLevel"].ToString();
              }
          }

        }
      }


这篇关于Textbox TextChanged正在激活但不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆