如何将自动完成扩展器绑定到动态创建的控件

本文关键字:绑定 动态 创建 控件 扩展器 | 更新日期: 2023-09-27 18:36:09

自动填充适用于静态文本框,但不适用于动态文本框。
用户需要能够根据需要添加任意数量的行,并且每个文本框都应从同一表中提取。 表中的记录太多,无法使用简单的下拉列表...有什么想法吗?

这行得通

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