自动完成文本框没有填充其他字段

本文关键字:填充 其他 字段 文本 | 更新日期: 2023-09-27 18:10:26

我有麻烦得到一个TextBox与AutoCompleteExtender填充其他字段后选择已作出。如果我用下拉列表尝试同样的事情(改变控件名称,在适当的地方指向DDL而不是TextBox),它会完美地工作。

从下面的代码部分,谁能看到我可能做错了什么?我花了好几天试图让它工作!

ASPX

<asp:TextBox runat="server" 
    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="
                    // 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#代码后面

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();
    }
}

网络服务。ASMX (for AutoCompleteExtender)

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 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;
    }
}

自动完成文本框没有填充其他字段

对于我自己的问题,我有一个答案,我应该与你分享。上面的代码没有做我想要的原因是TextBoxes没有SelectedIndexChanged事件。他们有TextChanged事件!

将SelectedIndexChanged事件更改为TextChanged使我的代码能够无缝地工作。

感谢MetalASP。