Skip to content

Populate TextBoxes From An AutoComplete TextBox

Chris

Asked Jun 12, 2014, 7:23 AM EDT1 answer
Share
LinkedInX
I have a similar, but very different question to the one I posted yesterday, which was answered really well by Satyapriya Nayak. (http://www.c-sharpcorner.com/Forums/Thread/258270/populate-textboxes-from-a-dropdownlist.aspx)

I need to populate textboxes from the selection in an AutoCompleteExtender TextBox. I have partially managed to achieve this (the AutoComplete is working), but it is not filling the other TextBoxes.

Here is my code. I hope someone can help me again!

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

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



   


   
  

    
                                                    runat="server"
                            BehaviorID="AutoCompleteEx"
                            ID="autoComplete1"
                            TargetControlID="Top_Site_NumTextBox"
                            ServicePath="WebService.asmx"
                            ServiceMethod="GetSiteNum"
                            MinimumPrefixLength="2"
                            CompletionInterval="1000"
                            EnableCaching="true"
                            CompletionSetCount="20"
                            DelimiterCharacters=";, :"
                            ShowOnlyCurrentWordInCompletionListItem="true" >
                           
                               
                                   
                                        <%-- Make the completion list transparent and then show it --%>
                                       
                                       
                                       
                                        <%--Cache the original size of the completion list the first time the animation is played and then set it to zero --%>
                                       
                                       
                                        <%-- Expand from 0px to the appropriate size while fading in --%>
                                       
                                           
                                           
                                       
                                   
                               
                               
                                    <%-- Collapse down to 0px and fade out --%>
                                   
                                       
                                       
                                   
                               
                           
                       
     
     
     
     

   




ASPX.CS

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Services;

namespace Project_Review
{
    public partial class ProjectReviewAdd : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["Tesseract"].ConnectionString;
        string str;
        SqlCommand com;

        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox Top_Site_NumTextBox = FormViewAdd.FindControl("Top_Site_NumTextBox") as TextBox;

            Top_Site_NumTextBox.AutoPostBack = true;
            SqlConnection con = new SqlConnection(strConnString);

            if (!IsPostBack)
            {
                con.Open();
                str = "select Site_Num, Site_Name, Site_Address, Site_County, Site_Post_Code from SCSite";
                com = new SqlCommand(str, con);
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    Top_Site_NumTextBox.ToString();
                }
                reader.Close();
                con.Close();
            }
        }

        private void clear()
        {
            TextBox Top_Site_NameTextBox = FormViewAdd.FindControl("Top_Site_NameTextBox") as TextBox;
            TextBox Top_Site_AddressTextBox = FormViewAdd.FindControl("Top_Site_AddressTextBox") as TextBox;
            TextBox Top_Site_CountyTextBox = FormViewAdd.FindControl("Top_Site_CountyTextBox") as TextBox;
            TextBox Top_Site_PostcodeTextBox = FormViewAdd.FindControl("Top_Site_PostcodeTextBox") as TextBox;

            Top_Site_NameTextBox.Text = "";
            Top_Site_AddressTextBox.Text = "";
            Top_Site_CountyTextBox.Text = "";
            Top_Site_PostcodeTextBox.Text = "";

        }

        protected void Top_Site_NumTextBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox Top_Site_NumTextBox = FormViewAdd.FindControl("Top_Site_NumTextBox") as TextBox;
            TextBox Top_Site_NameTextBox = FormViewAdd.FindControl("Top_Site_NameTextBox") as TextBox;
            TextBox Top_Site_AddressTextBox = FormViewAdd.FindControl("Top_Site_AddressTextBox") as TextBox;
            TextBox Top_Site_CountyTextBox = FormViewAdd.FindControl("Top_Site_CountyTextBox") as TextBox;
            TextBox Top_Site_PostcodeTextBox = FormViewAdd.FindControl("Top_Site_PostcodeTextBox") as TextBox;

            clear();
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            str = "select Site_Num, Site_Name, Site_Address, Site_County, Site_Post_Code from SCSite where Site_Num='" + Top_Site_NumTextBox.Text + "'";
            com = new SqlCommand(str, con);
            SqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                Top_Site_NameTextBox.Text = reader["Site_Name"].ToString();
                Top_Site_AddressTextBox.Text = reader["Site_Address"].ToString();
                Top_Site_CountyTextBox.Text = reader["Site_County"].ToString();
                Top_Site_PostcodeTextBox.Text = reader["Site_Post_Code"].ToString();

            }
            reader.Close();
            con.Close();
        }
    }
}


WEBSERVICE.ASMX

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace ProjectReview.ProjectReview
{
    ///
    /// Summary description for WebService
    ///
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        public WebService()
        {
            //Uncomment the following line if using designed components
            //InitializeComponent();
        }
        [WebMethod]
        public List GetSiteNum(string prefixText, int count)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Tesseract"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("select Site_Num, Site_Name from SCSite where Site_Num like @Site_Num+'%' ORDER BY Site_Num", con);
            cmd.Parameters.AddWithValue("@Site_Num", prefixText);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            List Site_Num = new List();
            List Site_Name = new List();
            for (int i = 0; i             {
                Site_Num.Add(dt.Rows[i][0].ToString());
                Site_Name.Add(dt.Rows[i][0].ToString());
            }
            return Site_Num;
        }
    }
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    I have an answer to my own question, which I ought to share with you. The reason the above code was not doing what I wanted is that TextBoxes do not have a SelectedIndexChanged event. They have TextChanged events!

    Altering the SelectedIndexChanged event to TextChanged enabled my code to work seamlessly.

    Thanks to MetalASP.Net at forums.asp.net http://forums.asp.net/p/1994061/5725350.aspx?p=True&t=635394626796770435&pagenum=1

    0