Click here to Skip to main content
15,891,799 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
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.

C#
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:

ASP.NET
<%@ 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>
Posted
Updated 29-Oct-13 23:44pm
v2
Comments
Richard C Bishop 29-Oct-13 16:44pm    
For starters, you cannot reference the textbox text value onpageload, there is nothing in the textbox when the page renders. So if the rest of your code is relying on a task initiated in the pageload event, the code will not run as expected.
Computer Wiz99 29-Oct-13 17:37pm    
Ok, for starters the Page_Load is there for the check if the user exist. Thst us all it is there for. Not to populate the text box. The problem lies in theTextBoxEA_TextChanged. It does not fire when you click or press tge tab key.
Tom Marvolo Riddle 30-Oct-13 5:58am    
I check your code in my system.TextBoxEA_TextChanged get fired
Computer Wiz99 30-Oct-13 6:30am    
Did it populate the textboxes with data from the database?
What did you do differently?
Tom Marvolo Riddle 30-Oct-13 6:02am    
Put a breakpoint in textchanged event and check it.

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" />
 
Share this answer
 
v3
Comments
Computer Wiz99 29-Oct-13 21:35pm    
S.M. Shasan Habib, thanks for the info but the AutoPostBack is set to true.
S. M. Ahasan Habib 29-Oct-13 22:54pm    
Please show your mark-up code. Also need to know any client side validation logic exists which prevent form being post back to server.
Computer Wiz99 30-Oct-13 5:43am    
I have added the mark-up code.
S. M. Ahasan Habib 30-Oct-13 6:21am    
I copy your markup and added it to my solution and found it is working as you expect(after enter email to the first textbox it will postback to the server). Only that thing is different from my code.
Your mark-up top line is
<%@ Page Language="C#" AutoEventWireup="True" Inherits="SubmitPage" Codebehind="SubmitPage.aspx.cs" %>
in my case it is
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
In my test project it is .NET framework 4.5 with visual studio 2012.
I request you to check the Inherits property to your markup top line. It must be same as your code behind class name (namespace.classname is actual format).
Another thing you can do
Just remove all code and add a single textbox there and check it is working or not. If works then add more control and check. Better you create another aspx page and do rnd there with that way. Hope you can solve the problem. Just be confident that code is fine(your code is working in my side).
Nothing wrong with the textchanged event.it get fired i checked it.

C#
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.
 
Share this answer
 
v2
Comments
Computer Wiz99 30-Oct-13 8:46am    
Jas24, What do you mean? I have two connection strings to handle each one.
Tom Marvolo Riddle 30-Oct-13 9:08am    
Thats not a matter here.dr[] value is being replaced by dr2[]value.Textbox will show the last assigned value
Computer Wiz99 30-Oct-13 9:12am    
Ok. So I know I am working with two different controls but with the DropDownList I can use the same code and it works. What makes the textbox control different? Plus, What is the best way to fix this? Can the textbox control populate data into other textboxes from a database?
Tom Marvolo Riddle 30-Oct-13 9:16am    
I guess you want to assign value to textbox from database in textchanged event.am i right?
Tom Marvolo Riddle 30-Oct-13 9:21am    
If so,try with single textbox with textchanged event.Then rest will be done easily
this link helps:

http://www.aspsnippets.com/Articles/Simple-User-Registration-Form-Example-in-ASPNet.aspx
 
Share this answer
 
Ok. After reading and testing and reading the comments here and testing. I have it working. The only problem is that when I enter a different email address and I click or tab into the next textbox, nothing changes. Why is that? Here is my code:

C#
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();
            

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900