Skip to content

TextBox Troubles

Chris

Asked Jun 27, 2014, 1:54 AM EDT1 answer
Share
LinkedInX

I am having trouble getting a TextBox with AutoCompleteExtender to populate other fields after a selection has been made. If I try the same thing with a DropDownList (changing the control names where appropriate to point to the DDL instead of TextBox) it works perfectly.

From the below sections of code can anyone see what I may be doing wrong? I have spent days trying to get it to work!
 
ASPX
 

<asp:TextBox runat="server"

JavaScript
                            ID="Top_Site_NumTextBox"
                            Size="6"
                            AutoPostBack="true"
                            AppendDataBoundItems="true"
                            DataTextField="Site_Num"
                            DataValueField="Site_Num"
                            DataSourceID="SqlDataSourceSite"
                            OnSelectedIndexChanged="Top_Site_NumTextBox_SelectedIndexChanged"
                            Text='<%# Bind("Top_Site_Num") %>' />
                        <asp:AutoCompleteExtender
                            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" >

<Animations>

<OnShow>

<Sequence>

<%-- Make the completion list transparent and then show it --%>

<OpacityAction Opacity="0" />

<HideAction Visible="true" />

<%--Cache the original size of the completion list the first time the animation is played and then set it to zero --%>

<ScriptAction Script="

C#
                                            // Cache the size and setup the initial size
                                            var behavior = $find('AutoCompleteEx');
                                            if (!behavior._height) {
                                            var target = behavior.get_completionList();
                                            behavior._height = target.offsetHeight - 2;
                                            target.style.height = '0px';

}" />

<%-- Expand from 0px to the appropriate size while fading in --%>

<Parallel Duration=".4">

<FadeIn />

<Length PropertyKey="height" StartValue="0" EndValueScript="$find('AutoCompleteEx')._height" />

</Parallel>

</Sequence>

</OnShow>

<OnHide>

<%-- Collapse down to 0px and fade out --%>

<Parallel Duration=".4">

<FadeOut />

<Length PropertyKey="height" StartValueScript="$find('AutoCompleteEx')._height" EndValue="0" />

</Parallel>

</OnHide>

</Animations>

</asp:AutoCompleteExtender>

 
 
C# CODEBEHIND
 
C#
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, Cust_Cust_Ref from SCSite, SCCust where Site_Cust_Num = Cust_Num order by Site_Num";
                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;
            TextBox Top_CustTextBox = FormViewAdd.FindControl("Top_CustTextBox") as TextBox;
            Top_Site_NameTextBox.Text = "";
            Top_Site_AddressTextBox.Text = "";
            Top_Site_CountyTextBox.Text = "";
            Top_Site_PostcodeTextBox.Text = "";
            Top_CustTextBox.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;
            TextBox Top_CustTextBox = FormViewAdd.FindControl("Top_CustTextBox") as TextBox;
            clear();
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            str = "select Site_Num, Site_Name, Site_Address, Site_County, Site_Post_Code, Cust_Cust_Ref from SCSite, SCCust where Site_Cust_Num = Cust_Num AND 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();
                Top_CustTextBox.Text = reader["Cust_Cust_Ref"].ToString();
            }
            reader.Close();
            con.Close();
        }
 

WEBSERVICE.ASMX (for the AutoCompleteExtender) 

PowerShell
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>
    /// Summary description for WebService
    /// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

C#
    // 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]

Java
        public List<string> GetSiteNum(string prefixText, int count)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Tesseract"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("select Site_Num 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<string> Site_Num = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Site_Num.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