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" %>
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
{
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
List
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;
}
}
}
ChrisPosted Jun 27, 2014, 10:54 AM
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