清除文本框的代码不起作用 [英] Code for clearing textbox not working

查看:61
本文介绍了清除文本框的代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,在显示错误消息并且用户点击确定按钮后清除表单上的三个文本框。 textbox1正在清除,但textbox2和3未清除。为什么会这样?



I have a code to clear three textboxes on the form after an error message has displayed and user clicks on the ok button. The textbox1 is clearing but textbox2 and 3 are not clearing. Why is this happening?

protected void Page_Load(object sender, EventArgs e)
    {
        
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "Select count(*) from Table1 where EmailAddress='" + TextBoxEA.Text + "'";
            string cmdStr2 = "Select count(*) from Table2 where EmailAddress='" + TextBoxEA.Text + "'";
            string cmdStr3 = "Select count(*) from Table3 where EmailAddress='" + TextBoxEA.Text + "'";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand userExist2 = new SqlCommand(cmdStr2, con);
            SqlCommand userExist3 = new SqlCommand(cmdStr3, con);
            SqlCommand cmd = new SqlCommand("select INST_ID, EmailAddress from Table1", con);
            SqlCommand cmd2 = new SqlCommand("select INST_ID, EmailAddress from Table2", con);
            SqlCommand cmd3 = new SqlCommand("select INST_ID, EmailAddress from Table3", con);
            userExist.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
            userExist.Parameters.AddWithValue("@Password", TextBoxPW.Text);
            userExist2.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
            userExist2.Parameters.AddWithValue("@Password", TextBoxPW.Text);
            userExist3.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
            userExist3.Parameters.AddWithValue("@Password", TextBoxPW.Text);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            int temp2 = Convert.ToInt32(userExist2.ExecuteScalar().ToString());
            int temp3 = Convert.ToInt32(userExist3.ExecuteScalar().ToString());

            if (temp == 1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('User Name Already Exist!!!');", true);
                TextBoxEA.Text = string.Empty;
                TextBoxINST_ID.Text = string.Empty;
                TextBoxaccessLevel.Text = string.Empty;
                TextBoxEA.Focus();
                
            }
            else if (temp2 == 1)
            {
                
            }
            else if (temp3 == 1)
            {
                
            }
            else if (temp2 == 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('User Name Is Not Recognized by The System!!!');", true);
                TextBoxEA.Text = string.Empty;
                TextBoxINST_ID.Text = string.Empty;
                TextBoxaccessLevel.Text = string.Empty;
                TextBoxEA.Focus();
                
            }
            else if (temp3 == 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('User Name Is Not Recognized by The System!!!');", true);
                TextBoxEA.Text = string.Empty;
                TextBoxINST_ID.Text = string.Empty;
                TextBoxaccessLevel.Text = string.Empty;
                TextBoxEA.Focus();
                
            }
            con.Close();
        }
    }

推荐答案

如果你没有做同样的事情,它会真的帮你调试连续3次......你知道疯狂的定义是什么吗? 一遍又一遍地做同样的事情并期待不同的结果。



您的整个代码可以归结为:



It would really help you debug if you didn't do the exact same thing 3 times in a row... Do you know what the definition of insanity is? "Doing the same thing over and over and expecting different results".

Your entire code can be boiled down to this:

protected void Page_Load(object sender, EventArgs e)
    {
        
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "Select count(*) from Table1 where EmailAddress=@EmailAddress";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            userExist.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
            int temp = (int)userExist.ExecuteScalar();
 
            if (temp == 1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('User Name Already Exist!!!');", true);
                TextBoxEA.Text = string.Empty;
                TextBoxINST_ID.Text = string.Empty;
                TextBoxaccessLevel.Text = string.Empty;
                TextBoxEA.Focus();
                
            }
            con.Close();
        }
    }





尝试运行它,看看你是否得到了预期的结果。你正在做很多奇怪的事情,比如ExecuteScalar()。ToString()然后当ExecuteScalar直接返回一个整数(在这个例子中,你的查询中的Count(*))然后将它转换回整数。您还要创建一个命令并向其添加参数,但是您从不在select语句中定义它们。



Try running that and see if you get the desired results. You are doing a lot of weird things, like ExecuteScalar().ToString() and then converting it back to an integer when ExecuteScalar already returns an integer directly (in this instance, the Count(*) from your query). You are also creating a command and adding parameters to it, but you never define them in the select statement.


这篇关于清除文本框的代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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