如何将 AutoCompleteExtender 绑定到动态创建的控件?

How can I bind AutoCompleteExtender to dynamically created control?

自动填充适用于静态文本框,但不适用于动态文本框。
用户需要能够根据需要添加尽可能多的行,并且每个文本框都应该来自同一个 table。 table 中的记录太多,无法使用简单的下拉菜单...有什么想法吗?

这个有效

<form id="TestForm" runat="server">
    <asp:ScriptManager ID="thisNameDoesntMatter" runat="server"
        EnablePageMethods="true" />
    <asp:TextBox ID="noteValue" runat="server" OnTextChanged="noteValue_TextChanged" CausesValidation="true" Width="1000"></asp:TextBox>
    <cc1:AutoCompleteExtender ServiceMethod="searchNoteValues"
                        MinimumPrefixLength="2"
                        CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
                        TargetControlID="noteValue"
                        ID="AutoCompleteExtenderNoteValues" runat="server" FirstRowSelected="false"/>

</form>

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]

    public static List<string> searchNoteValues(string prefixText, int count)
    {
        string strConn = db_connection_string_EINSTEIN;
        SqlConnection con = new SqlConnection(strConn);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT id, NoteValue FROM agl.GuidelineNoteValues where isActive = 1 and NoteValue like '%' + @SearchText + '%'";
        cmd.Parameters.AddWithValue("@SearchText", prefixText);
        con.Open();
        List<string> NoteValues = new List<string>();
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            while (sdr.Read())
            {
                NoteValues.Add(sdr["NoteValue"].ToString());
            }
        }
        con.Close();
        return NoteValues;
    }

这不是

    <form id="TestForm" runat="server">
    <asp:ScriptManager ID="thisNameDoesntMatter" runat="server"
        EnablePageMethods="true" />        
    <asp:PlaceHolder ID="TestPlaceHolder" runat="server" />
    <asp:LinkButton ID="TestAddNoteButton" runat="server" Text="Add a note" OnClick="TestAddNoteButton_Click" CausesValidation="false" AutoPostBack="true"/>
    </div>

</form>

    Table notesTable = new Table();

    protected void Page_PreRender(object sender, EventArgs e)
    {
        Session["table"] = notesTable;
    }

    public void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            notesTable.ID = "MyTable";
        }
        else
        {
            notesTable = (Table)Session["table"];
            TestPlaceHolder.Controls.Add(notesTable);

        }
    }


    protected void TestAddNoteButton_Click(object sender, EventArgs e)
    {
        TableRow tr = new TableRow();
        TableCell tc1 = new TableCell();
        TableCell tc2 = new TableCell();
        string strConn = db_connection_string_EINSTEIN;
        SqlConnection con = new SqlConnection(strConn);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT id, name FROM agl.guidelineNoteCategories";
        con.Open();
        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        dAdapter.Fill(objDs);
                if (objDs.Tables[0].Rows.Count > 0)
                {
                    System.Web.UI.WebControls.ListBox lb = new System.Web.UI.WebControls.ListBox();
                    System.Web.UI.WebControls.TextBox tb = new System.Web.UI.WebControls.TextBox();// { ID = sna.ToString()};
                    tb.Width = 1000;
                    tb.ID = "tb";

                    AjaxControlToolkit.AutoCompleteExtender ace = new AjaxControlToolkit.AutoCompleteExtender();                        
                    ace.ServiceMethod="searchNoteValues";
                    ace.MinimumPrefixLength=2;
                    ace.CompletionInterval=100;
                    ace.EnableCaching=false;
                    ace.CompletionSetCount=10;
                    ace.TargetControlID="tb";
                    ace.FirstRowSelected=false;

                    lb.BorderColor = System.Drawing.Color.Orange;
                    lb.DataSource = objDs.Tables[0];
                    lb.DataTextField = "name";
                    lb.DataValueField = "id";
                    lb.DataBind();
                    lb.Items.Insert(0, "--Select--");
                    tc1.Controls.Add(lb);
                    tc2.Controls.Add(tb);
                    tr.Cells.Add(tc1);
                    tr.Cells.Add(tc2);
                    notesTable.Rows.Add(tr);
                    Session["table"] = notesTable;
                    ViewState["dynamictable"] = true;

                }
            con.Close();
        }


    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]

    public static List<string> searchNoteValues(string prefixText, int count)
    {
        string strConn = db_connection_string_EINSTEIN;
        SqlConnection con = new SqlConnection(strConn);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT id, NoteValue FROM agl.GuidelineNoteValues where isActive = 1 and NoteValue like '%' + @SearchText + '%'";
        cmd.Parameters.AddWithValue("@SearchText", prefixText);
        con.Open();
        List<string> NoteValues = new List<string>();
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            while (sdr.Read())
            {
                NoteValues.Add(sdr["NoteValue"].ToString());
            }
        }
        con.Close();
        return NoteValues;
    }

}

知道了。没有将自动完成扩展程序添加到表单。

                    TestForm.Controls.Add(ace);