Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have a web form that has a Label and two textboxes. One of the textboxes you can enter a name and press enter and the Label will populate with the data from the database but the textbox will not. I am trying to get the textbox to populate with the same data that the Label populates. How can I do that? What did I do wrong?

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        #form1
        {
            text-align: right;
        }
        .style1
        {
            width: 98%;
        }
        .style3
        {
            width: 1096px;
            text-align: left;
        }
        .style5
        {
            width: 585px;
            height: 137px;
        }
        .style6
        {
            width: 100%;
        }
        .style7
        {
            width: 68px;
        }
        .style8
        {
            font-weight: bold;
        }
        </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">
    
        <table class="style1">
            <tr>
                <td class="style3">
                    <table class="style1">
                        <tr>
                            <td style="text-align: justify">
                                                                                              
                                <img class="style5" src="Images/buildout_header.jpg" /></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    
    </div>
    <p>
         </p>
    <table class="style6">
        <tr>
            <td class="style7" style="text-align: left">
                <asp:Label ID="Label1" runat="server" CssClass="style8" Text="INSTID"></asp:Label>
            </td>
            <td style="text-align: left">
                <asp:Label ID="Label2" runat="server" CssClass="style8" Text="INSTITUTION"></asp:Label>
            </td>
            <td style="text-align: left">
                 </td>
        </tr>
        <tr>
            <td class="style7" style="text-align: left">
                <asp:Label ID="lblINST_ID" runat="server" style="font-weight: 700"></asp:Label>
            </td>
            <td style="text-align: left">
                <asp:TextBox ID="TextBoxLongName" runat="server" 
                    ontextchanged="TextBoxLongName_TextChanged" Width="415px"></asp:TextBox>
            </td>
            <td style="text-align: left">
                <asp:TextBox ID="TextBoxINST_ID" runat="server"></asp:TextBox>
            </td>
        </tr>
    </table>
    <br />
    <p>
         </p>
    </form>
</body>
</html>


C#
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Printing;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace SACSCOCLogin1._1
{
    public partial class BranchOffsiteCamp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

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

            
            SqlCommand scmd = new SqlCommand("Select INST_ID, LongName from Table23 where LongName = '" + TextBoxLongName.Text + "'", con);
            
            SqlDataReader dr = scmd.ExecuteReader();
          
            if (dr.Read())
                
            {
                lblINST_ID.Text = dr["INST_ID"].ToString();
                TextBoxINST_ID.Text = dr["INST_ID"].ToString(); 
            }

            

            dr.Close();
            con.Close();
        }
    }
}
Posted
Comments
CHill60 13-May-14 7:46am    
You marked the solution to your previous post as the Answer. Are you now saying it didn't work?
Computer Wiz99 13-May-14 8:24am    
On the previous question it did not populate the textbox so I changed the textbox to a Label and it worked. Now when I try to populate a textbox with the same data that the Label is populated with nothing comes up. Should I delete the web form and try a new one for this might have errors on it?
CHill60 13-May-14 10:41am    
When I run you code as is then neither the label nor the textbox are updated as soon as the text changes in TextBoxLongName. However if I put AutoPostBack="True" as an attribute of TextBoxLongName then both work.
Computer Wiz99 13-May-14 10:44am    
Yep!! I just thought about that and I did it also. Thanks for your help. Post solution please.
CHill60 13-May-14 11:41am    
Done!

1 solution

When I run you code as is then neither the label nor the textbox are updated as soon as the text changes in TextBoxLongName.

However if I put AutoPostBack="True" as an attribute of TextBoxLongName then both work.
 
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
Top Experts
Last 24hrsThis month


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