How To select values on initial page load

5 posts, 0 answers
  1. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 23 Aug 2012 Link to this post

    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.
  2. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 24 Aug 2012 Link to this post

    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));
       }
    }
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ivana
    Admin
    Ivana avatar
    657 posts

    Posted 27 Aug 2012 Link to this post

    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.
  5. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 27 Aug 2012 Link to this post

    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>
  6. Ivana
    Admin
    Ivana avatar
    657 posts

    Posted 29 Aug 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017