Chris

Chris

  • NA
  • 16
  • 4.1k

Populate TextBoxes From An AutoComplete TextBox

Jun 12 2014 11:23 AM
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">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
   <div>
     <asp:TextBox runat="server" ID="Top_Site_NumTextBox" Size="6" AutoPostBack="true" OnSelectedIndexChanged="Top_Site_NumTextBox_SelectedIndexChanged" TextBox='<%# 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="
                                            // 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>
      <asp:TextBox ID="Top_Site_NameTextBox" runat="server" Text='<%# Bind("Top_Site_Name") %>' Width="95%" />
      <asp:TextBox ID="Top_Site_AddressTextBox" runat="server" Height="4em" Text='<%# Bind("Top_Site_Address") %>' TextMode="MultiLine" Width="95%" />
      <asp:TextBox ID="Top_Site_CountyTextBox" runat="server" Text='<%# Bind("Top_Site_County") %>' />
      <asp:TextBox ID="Top_Site_PostcodeTextBox" runat="server" Text='<%# Bind("Top_Site_Postcode") %>' />
</div>
    </form>
</body>
</html>



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>
    /// Summary description for WebService
    /// </summary>
    [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<string> 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<string> Site_Num = new List<string>();
            List<string> Site_Name = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Site_Num.Add(dt.Rows[i][0].ToString());
                Site_Name.Add(dt.Rows[i][0].ToString());
            }
            return Site_Num;
        }
    }
}

Answers (1)