如何在listview中自动完成文本框?

时间:2012-03-20 11:12:54

标签: c# asp.net listview autocomplete ajaxcontroltoolkit

这里我在文本框中使用自动完成,值来自database.It工作正常。我需要的是在listview中输入任何值时在listview中执行此自动完成功能。这是我的代码

  <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
    <cc1:AutoCompleteExtender ServiceMethod="ListView1_SelectedIndexChanged" 
        MinimumPrefixLength="2"
        CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" 
        TargetControlID="txtContactsSearch"
        ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
    </cc1:AutoCompleteExtender>

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                <tr id="Tr1" runat="server">
                    <td id="Td1" runat="server">
                        <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("regon_name") %>' />
                    </td>
                      <td id="Td2" runat="server">
                        <asp:Label ID="Label3" runat="server" Text='<%#Eval("code") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DB_CGTPO_DEVEConnectionString %>"
            SelectCommand="SELECT [regon_name], [code] FROM [region_test]"></asp:SqlDataSource>

 [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> ListView1_SelectedIndexChanged(string prefixText, int count)
    {
        using (SqlConnection conn = new SqlConnection(""))
        {
            //conn.ConnectionString = ConfigurationManager
            //        .ConnectionStrings["constr"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select regon_name from region_test where " +
                "regon_name like @SearchText + '%'";
                cmd.Parameters.AddWithValue("@SearchText", prefixText);
                cmd.Connection = conn;
                conn.Open();
                List<string> customers = new List<string>();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {

                        customers.Add(sdr["regon_name"].ToString());
                    }
                }
                conn.Close();
                return customers;
            }
        }
    }

有什么建议吗?

1 个答案:

答案 0 :(得分:-1)

为什么不把文本框放在listview中??

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                <tr id="Tr1" runat="server">
                    <td id="Td1" runat="server">
                        <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("regon_name") %>' />
                    </td>
                      <td id="Td2" runat="server">
                        <asp:Label ID="Label3" runat="server" Text='<%#Eval("code") %>' />
                    </td>
                </tr>
<tr>
 <td id="Td3" runat="server" colspan="2">
 <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
    <cc1:AutoCompleteExtender ServiceMethod="ListView1_SelectedIndexChanged" 
        MinimumPrefixLength="2"
        CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" 
        TargetControlID="txtContactsSearch"
        ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
    </cc1:AutoCompleteExtender>
 </td>
</tr>
            </ItemTemplate>
        </asp:ListView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DB_CGTPO_DEVEConnectionString %>"
            SelectCommand="SELECT [regon_name], [code] FROM [region_test]"></asp:SqlDataSource>