简体   繁体   中英

How can I bind AutoCompleteExtender to dynamically created control?

The autofill works for the static text box, but not the dynamic one.
User needs to be able to add as many rows as necessary, and each text box should pull from the same table. There are too many records in the table to use a simple drop down... Any ideas?

This Works

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

This Does Not

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

}

Got it. Wasn't adding the auto complete extender to the form.

                    TestForm.Controls.Add(ace);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM