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

How To select values on initial page load

4 Answers 410 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 24 Aug 2012, 04:24 AM
Pretty common operation I would have thought, but I can find nothing in the documentation: http://www.telerik.com/help/aspnet-ajax/autocompletebox-overview.html 

There is no setter on the Text property, and it seems like I need some kind of FindEntry() method.

4 Answers, 1 is accepted

Sort by
0
Jeremy
Top achievements
Rank 1
answered on 24 Aug 2012, 05:34 AM
OK I changed the InputType to Token and I am now doing Entry.Add() to add the new values. This works but is not robust as it assumes the preselected values are valid. I prefer the FindItemByValue method of the RadCombobox - surely there is something similar for the autocompletebox?

ie:
if (!Page.IsPostBack)
{
   string
[] preselectedValuesArray = {"xxx","xyz"};
   for (int index = 0; index < preselectedValuesArray.Length; index++)
   {
     string tmpString = preselectedValuesArray[index];
     preselectedValuesArray.Entries.Add(new AutoCompleteBoxEntry(tmpString));
   }
}
0
Ivana
Telerik team
answered on 27 Aug 2012, 01:20 PM
Hello Jeremy,

You could try the following approach to preselect entries in the initial page load of the page:
<telerik:RadAutoCompleteBox ID="RadAutoCompleteBox2" runat="server">
</telerik:RadAutoCompleteBox>
protected void Page_Load(object sender, EventArgs e)
{
    DataTable dataTable = DataBindMethod();
    RadAutoCompleteBox2.DataTextField = "Name";
    RadAutoCompleteBox2.DataValueField = "ID";
    RadAutoCompleteBox2.DataSource = dataTable;
    if (!Page.IsPostBack)
    {
        foreach (DataRow dataRow in dataTable.Rows)
        {
            AutoCompleteBoxEntry entry = new AutoCompleteBoxEntry();
            entry.Text = (string)dataRow["Name"];
            entry.Value = dataRow["ID"].ToString();
            if (entry.Value == "1")
                RadAutoCompleteBox2.Entries.Add(entry);
        }
    }
}
 
private DataTable DataBindMethod()
{
    string sqlSelectCommand = "SELECT [ID], [CountryID], [Name] FROM [Cities]";
    SqlDataAdapter adapter = new SqlDataAdapter(sqlSelectCommand,
        ConfigurationManager.ConnectionStrings["TelerikConnectionString"].ConnectionString);
    DataTable dataTable = new DataTable();
    adapter.Fill(dataTable);
 
    return dataTable;
}

I hope this will help.

Regards,
Ivana
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Jeremy
Top achievements
Rank 1
answered on 28 Aug 2012, 03:24 AM
Hi Ivana

I can see that the option you have provided will work, however I am concerned that it will be accessing the database unnecessarily (ie EVERY page load). There is a lot of other stuff going on in the page, and the network is pretty slow, so I'd like to cut that down. I am currently using an object datasource to access my DAL and I presume it only accesses it when the user focuses on the textbox?
I guess I can just manually validate the preselections on the first page load instead.

This is the code for my autocompletebox btw:
<telerik:RadAutoCompleteBox ID="RadAutoCompleteBox1" runat="server"
   Width="280px"
   Skin="Vista"
   DataSourceID="ds1"
   InputType="Token"
   OnClientEntryAdding="OnClientEntryAddingHandler">
</telerik:RadAutoCompleteBox>
 
<asp:ObjectDataSource ID="ds1" runat="server" TypeName="DAL.dalCities" SelectMethod="GetCitiesByState">
   <SelectParameters>
      <asp:Parameter Name="state" Type="String" DefaultValue="WA" />
   </SelectParameters>
</asp:ObjectDataSource>
0
Ivana
Telerik team
answered on 29 Aug 2012, 01:00 PM
Hello Jeremy,

The code I have provided you with in my previous post does fetch the items from the data base only on the  very fist load of the page in order to make the preselection of the entries. The next request to the data base is made upon request initiated from the user i.e when the user starts typing into the text area. 

The main idea of the RadAutoCompleteBox is to work with auto complete scenario and loading data only when needed. It is not intended to give a possibility to work with the drop-down items at any time.
For more information, you can take a look at the following blog post explaining more on the main idea of RadComboBox: http://blogs.telerik.com/aspnet-ajax/posts/12-08-06/the-best-fit-for-your-project---telerik-s-asp-net-ajax-combobox-or-autocompletebox.aspx.

I hope this will be helpful

Regards,
Ivana
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
AutoCompleteBox
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Jeremy
Top achievements
Rank 1
Ivana
Telerik team
Share this question
or