TextBox TextChanged没有使用C#在ASP.NET中触发 [英] TextBox TextChanged not firing in ASP.NET using C#

查看:51
本文介绍了TextBox TextChanged没有使用C#在ASP.NET中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个TextBox_TextChanged但它不起作用。我想要做的是当用户在文本框中输入电子邮件地址时,文本框将AutoPostBack并填充表单上的其他两个文本框。我已经尝试了所有我能想到的东西,它仍然无法运作。请帮助我,因为现在我被卡住了。



I have a TextBox_TextChanged on a form and It doesn't work. What I am trying to do is when a user puts in an Email Address into the textbox the textbox will AutoPostBack and populate two other textboxes on the form. I have tried everything I can think of and it still will not work. Please help me because now I am stuck.

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();
            SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con2.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 + "'", 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 the mark-up code:

<%@ Page Language="C#" AutoEventWireup="True" Inherits="SubmitPage" Codebehind="SubmitPage.aspx.cs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style4
        {
            width: 579px;
            text-align: right;
        }
        .style5
        {
            width: 202px;
        }
        .style6
        {
            width: 579px;
        }
        .style7
        {
            width: 585px;
            height: 137px;
        }
        #form1
        {
            text-align: center;
        }
        .style8
        {
            text-align: left;
        }
    </style>
</head>
<body background="Images/bkg-blu.jpg">
    <form id="form1" runat="server">
    <div style="font-size: x-large; font-weight: 700; text-align: center">
    
        <div style="text-align: center">
            <img class="style7" src="Images/buildout_header.jpg" /></div>
        <br />
        <br />
        <br />
    
<asp:Label ID="Label1" runat="server" Text="SACSCOC User Registration"></asp:Label><br />
        <br />
        <br />
        <br />
    
    </div>
    <table class="style1">
        <tr>
            <td class="style4">
                <asp:Label ID="Label2" runat="server" Text="UserName"></asp:Label></td><td class="style5">
                <asp:TextBox 

                    ID="TextBoxEA" runat="server" Width="180px" AutoPostBack="True" 

                    ontextchanged="TextBoxEA_TextChanged"></asp:TextBox></td><td class="style8">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

                    ControlToValidate="TextBoxEA" ErrorMessage="You Must Have a Valid UserName" 

                    ForeColor="Red"></asp:RequiredFieldValidator></td></tr><tr>
            <td class="style4">
                <asp:Label ID="Label3" runat="server" Text="Password"></asp:Label></td><td class="style5">
                <asp:TextBox ID="TextBoxPW" runat="server" Width="180px" TextMode="Password"></asp:TextBox></td><td class="style8">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 

                    ControlToValidate="TextBoxPW" ErrorMessage="You Must Have a Valid Password" 

                    ForeColor="Red"></asp:RequiredFieldValidator></td></tr><tr>
            <td class="style4">
                Retype Password</td><td class="style5">
                <asp:TextBox ID="TextBoxRPassword" runat="server" Width="180px" 

                    TextMode="Password"></asp:TextBox></td><td class="style8">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 

                    ControlToValidate="TextBoxRPassword" ErrorMessage="You Must Have a Valid Password" 

                    ForeColor="Red"></asp:RequiredFieldValidator><br />
                <asp:CompareValidator ID="CompareValidator1" runat="server" 

                    ControlToCompare="TextBoxPW" ControlToValidate="TextBoxRPassword" 

                    ErrorMessage="Both Password Must Match!!!" ForeColor="Red"></asp:CompareValidator></td></tr><tr>
            <td class="style6">
                </td>
            <td class="style5">
                <asp:Button ID="Submit" runat="server" Text="Submit" Width="80px" 

                    onclick="Submit_Click" />
                     </td><td>
                </td>
        </tr>
    </table>
    <asp:Label ID="lblMessage" runat="server" ForeColor="Red" 

        style="font-weight: 700; font-size: large"></asp:Label><br />
    <table class="style1">
        <tr>
            <td style="text-align: left"><asp:TextBox ID="TextBoxINST_ID" runat="server" 

                    style="text-align: left"></asp:TextBox></td><td>
                 </td></tr><tr>
            <td style="text-align: left"><asp:TextBox ID="TextBoxaccessLevel" runat="server" style="text-align: left"></asp:TextBox></td><td>
                 </td></tr></table><p>
         </p></form></body></html>

推荐答案

for text box text change event fire, you need to set auto postback property to true. Eitherwise it will not fire. By the way it will fire when you press tab key or lost its focus from this textbox. It is like lost focus of windows application control event.





<asp:TextBox AutoPostBack=\"True\" />
for text box text change event fire, you need to set auto postback property to true. Eitherwise it will not fire. By the way it will fire when you press tab key or lost its focus from this textbox. It is like lost focus of windows application control event.


<asp:TextBox AutoPostBack="True" />


Nothing wrong with the textchanged event.it get fired i checked it.



Nothing wrong with the textchanged event.it get fired i checked it.

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();
               }





In this snippet dr2[\"INST_ID\"] value will be assigned to this TextBoxINST_ID not dr[\"INST_ID\"] value.

likewise this TextBoxaccessLevel textbox also having this issue.



In this snippet dr2["INST_ID"] value will be assigned to this TextBoxINST_ID not dr["INST_ID"] value.
likewise this TextBoxaccessLevel textbox also having this issue.


this link helps:



http://www.aspsnippets.com/Articles/Simple-User-Registration-Form-Example-in-ASPNet.aspx
this link helps:

http://www.aspsnippets.com/Articles/Simple-User-Registration-Form-Example-in-ASPNet.aspx


这篇关于TextBox TextChanged没有使用C#在ASP.NET中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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