This is a migrated thread and some comments may be shown as answers.

Better understanding of AutoCompleteBox and Postbacks

1 Answer 96 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Al
Top achievements
Rank 1
Al asked on 28 Dec 2012, 04:25 AM

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


1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Dec 2012, 07:21 AM
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.
Tags
AutoCompleteBox
Asked by
Al
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or