Better understanding of AutoCompleteBox and Postbacks

2 posts, 0 answers
  1. Al
    Al avatar
    19 posts
    Member since:
    Oct 2012

    Posted 27 Dec 2012 Link to this post


    I have the AutoCompletBox working on a form, but I want to make sure that I'm doing this right and that there isn't a more efficient way to use this control.

    It appears that we have to have this code block below in the postback section of the webpage in order for the AutoCompleteBox to work properly.  We are setting the DataSource to a datatable.  This datatable will end up having over 10,000 rows.  Since this has to be in the postback section to work, everytime the form does a post back it has to retrieve all of those rows whether the user is typing in the AutoCompleteBox or not.  And it will retrieve all 10,000 rows and then filter them down with every 3 second pause in the typing of the AutoCompleteBox.

    I assume that there is a better way to do this.  I suppose that we could use a webservice, but doesn't it still require all 10,000 rows to be pulled in each time even after the user has typed a few letters?  Am I correct that this tool always wants the full list and then it filters the rows down afterward?  Does it require this list of rows to be retrieved with each postback?

    Code block in postback:

    ACInSite.DataSource = SQLRowToDt(ConnStr, "Select siteName, siteID From site")
                        ACInSite.DataTextField = "siteName"
                        ACInSite.DataValueField = "siteID"
                        ACInSite.DataBind()


  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Dec 2012 Link to this post

    Hi,

    You can use webservice as follows to achieve your scenario.

    ASPX
    <telerik:RadAutoCompleteBox runat="server" ID="AutoCompletebox1" >
        <WebServiceSettings Path="AutoCompletebox.aspx" Method="GetCompanyNames" />
    </telerik:RadAutoCompleteBox>

    C#:
    [WebMethod]
        public static AutoCompleteBoxData GetCompanyNames(object context)
        {
            string searchString = ((Dictionary<string, object>)context)["Text"].ToString();
            DataTable data = GetChildNodes(searchString);
            List<AutoCompleteBoxItemData> result = new List<AutoCompleteBoxItemData>();
     
            foreach (DataRow row in data.Rows)
            {
                AutoCompleteBoxItemData childNode = new AutoCompleteBoxItemData();
                childNode.Text = row["ProductName"].ToString();
                childNode.Value = row["ProductID"].ToString();
                result.Add(childNode);
            }
     
            AutoCompleteBoxData res = new AutoCompleteBoxData();
            res.Items = result.ToArray();
     
            return res;
        }
     
        private static DataTable GetChildNodes(string searchString)
        {
            SqlCommand selectCommand = new SqlCommand(@"SELECT * FROM [Products] WHERE ProductName LIKE @ProductName + '%'");
            selectCommand.Parameters.AddWithValue("ProductName", searchString);
            return GetData(selectCommand);
        }
     
        private static DataTable GetData(SqlCommand selectCommand)
        {
            selectCommand.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
            DataTable data = new DataTable();
            adapter.Fill(data);
            return data;
        }

    Hope this helps.

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top