TextBox1_TextChanged不起作用 [英] TextBox1_TextChanged not working

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

问题描述

我有三个文本框。 Textbox1用户可以输入电子邮件地址。这也是我的TextChange所在的位置。 TextBox2和Textbox3是TextBox1更改时要显示的内容。因此,当用户在TextBox1中输入他们的电子邮件地址时,TextBox 2应显示UserID,TextBox3应显示Level。我确实将TextBox1 AutoPostBack设置为true。请帮忙!!我做错了什么?



<前lang =c#> 受保护 void TextBoxEA_TextChanged( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ PasswordConnectionString ] .ConnectionString);
con.Open();
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ PasswordConnectionString]的ConnectionString)。
con2.Open();

SqlCommand scmd = new SqlCommand( 从TableCEO中选择INST_ID,accessLevel,其中EmailAddress =' + TextBoxEA.Text + ' ,con);
SqlCommand scmd2 = new SqlCommand( 选择来自TableIALO的INST_ID,accessLevel,其中EmailAddress =' + TextBoxEA.Text + ' ,con2);
SqlDataReader dr = scmd.ExecuteReader();
SqlDataReader dr2 = scmd2.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();
con.Close();
dr2.Close();
con2.Close();
}

}





这是更新后的代码。我忘记了一些事情,它仍然无法奏效。我做错了什么?

解决方案

为TextBoxEA的 true .microsoft.com / zh-cn / library / system.web.ui.webcontrols.textbox.autopostback.aspx> AutoPostBack 属性


确定这不是解决方案。我发现了为什么它不起作用。我不得不重命名表中的一个字段。一旦我这样做,我得到了另一件事。当用户在EmailBoxEA中输入EmailAddress时,弹出的错误按钮会弹出:用户名已存在!!!。以下是整个表单的代码:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 System.Web.Security;
使用 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 = 选择计数(* )来自TableSecurity,其中EmailAddress =' + TextBoxEA.Text + ';
SqlCommand userExist = new SqlCommand(cmdStr,con);
SqlCommand cmd = new SqlCommand( select INST_ID,来自TableSecurity的电子邮件地址,con);
int temp = Convert.ToInt32(userExist.ExecuteScalar()。ToString());
if (temp == 0
{
lblMessage。 Text = 用户名已存在!!!;
}
}
}

受保护 void Submit_Click( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ PasswordConnectionString]。ConnectionString);
con.Open();
string cmdStr = 选择INST_ID,accessLevel ,来自TableCEO的EmailAddress,其中EmailAddress =' + TextBoxEA.Text + ';
string cmdStr2 = 选择INST_ID,accessLevel ,来自TableialO的EmailAddress,其中EmailAddress =' + TextBoxEA.Text + ';
string insCmd = 插入TableSecurity( EmailAddress,Password,INST_ID,accessLevel)值(@ EmailAddress,@ Password,@ INST_ID,@ accessLevel);
string insCmd2 = 插入TableSecurity( EmailAddress,Password,INST_ID,accessLevel)值(@ 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 (例外)
{
lblMessage.Text = 用户已存在;
}
最后
{
}
}

protected void TextBoxEA_TextChanged( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ PasswordConnectionString]。ConnectionString);
con.Open();
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ PasswordConnectionString]的ConnectionString)。
con2.Open();

SqlCommand scmd = new SqlCommand( 从TableCEO中选择INST_ID,accessLevel,其中EmailAddress =' + TextBoxEA.Text + ' ,con);
SqlCommand scmd2 = new SqlCommand( 选择来自TableIALO的INST_ID,accessLevel,其中EmailAddress =' + TextBoxEA.Text + ' ,con2);
SqlDataReader dr = scmd.ExecuteReader();
SqlDataReader dr2 = scmd2.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();
con.Close();
dr2.Close();
con2.Close();
}

}


I have three textboxes. Textbox1 user can enter email address. This is also where my TextChange is at. Textbox2 and Textbox3 is what I want to display when TextBox1 changes. So, when a user enter their email address into TextBox1, TextBox 2 should display UserID and TextBox3 should display Level. I do have the TextBox1 AutoPostBack set to true. Please help!! What did I do wrong?

protected void TextBoxEA_TextChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();
        SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con2.Open();

        SqlCommand scmd = new SqlCommand("Select INST_ID, accessLevel from TableCEO where EmailAddress = '" + TextBoxEA.Text + "'", con);
        SqlCommand scmd2 = new SqlCommand("Select INST_ID, accessLevel from TableIALO where EmailAddress = '" + TextBoxEA.Text + "'", con2);
        SqlDataReader dr = scmd.ExecuteReader();
        SqlDataReader dr2 = scmd2.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();
        con.Close();
        dr2.Close();
        con2.Close();
    }
   
}



Here is an Updated code. I forgot somethings and it still will not work. What did I do wrong?

解决方案

Set true for TextBoxEA's AutoPostBack property


Ok This is not a Solution. I found out why it didn't work. I had to rename a field in my table. Once I did that now I get the other thing going on. When a user enters in an EmailAddress into TextBoxEA, the error massage that I made pops up: "User Name Already Exist!!!". Here is the code for the whole form:

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 == 0)
            {
                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();
        SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con2.Open();

        SqlCommand scmd = new SqlCommand("Select INST_ID, accessLevel from TableCEO where EmailAddress = '" + TextBoxEA.Text + "'", con);
        SqlCommand scmd2 = new SqlCommand("Select INST_ID, accessLevel from TableIALO where EmailAddress = '" + TextBoxEA.Text + "'", con2);
        SqlDataReader dr = scmd.ExecuteReader();
        SqlDataReader dr2 = scmd2.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();
        con.Close();
        dr2.Close();
        con2.Close();
    }
   
}


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

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