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

RadAutoCompleteBox with default selected entries

4 Answers 1160 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Sypher
Top achievements
Rank 1
Sypher asked on 22 Aug 2012, 08:55 PM
I am trying to set up a RadAutoCompleteBox that pulls the current list of items from a data source and allows the user to add and remove from that list. So far in all of the examples I've seen, I can set the DataSourceID, DataTextField, and DataValueField to allow the user to add entries to the box, but I don't see a built-in way to start the RadAutoCompleteBox with entries in it.

For example, a RadComboBox used in a form can use SelectedIndex='<%#Bind("Field")%>' to select an item in the combo box 
using the parent form's data object. I would like to do something like that in the AutoCompleteBox and pass in a string of SelectedValues or something like that. Is that possible?

<telerik:RadAutoCompleteBox runat="server" ID="people"
    AllowCustomToken="true" InputType="Token"
    DataSourceID="dsPeople" DataTextField="Name" DataValueField="ID">
</telerik:RadAutoCompleteBox>

4 Answers, 1 is accepted

Sort by
0
Accepted
Ivana
Telerik team
answered on 27 Aug 2012, 12:31 PM
Hi Bryan,

Currently, RadAutoCompleteBox does not support preselecting items, coming from its data source, out-of-the-box. But, you still can manipulate the data source itself so you will be able to have preselected entries.

Here is an example of SqlDataSource:
<telerik:RadAutoCompleteBox ID="RadAutoCompleteBox2" runat="server" Width="200px"
    DropDownWidth="200px" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="ID"
    OnDataBound="RadAutoCompleteBox2_DataBound">
</telerik:RadAutoCompleteBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TelerikConnectionString %>"
    SelectCommand="SELECT [ID], [CountryID], [Name] FROM [Cities]" OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataSourceSelectArguments args = new DataSourceSelectArguments();
        DataView view = (DataView)SqlDataSource1.Select(args);
        DataTable table = view.ToTable();
 
        string ID = "", countryID = "", name = "";
        for (int i = 0; i < table.Rows.Count; i++)
        {
            ID = table.Rows[i].ItemArray[0].ToString();
            countryID = table.Rows[i].ItemArray[1].ToString();
            name = table.Rows[i].ItemArray[2].ToString();
 
            if (!string.IsNullOrEmpty(ID) && ID == "1")
            {
                if (!string.IsNullOrEmpty(name))
                {
                    RadAutoCompleteBox2.Entries.Add(new AutoCompleteBoxEntry(name, ID));
                }
            }
        }
    }
}

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
Sypher
Top achievements
Rank 1
answered on 07 Sep 2012, 07:59 PM
Cool, that basic idea worked for me. I had to make it more complicated by putting the RadAutoCompleteBox on the edit form of a RadGrid, but I just moved the Page_Load stuff to the OnDataBound of the RadAutoCompleteBox and got it working. From your example, it looks like you may have tried the OnDataBound as well.

It seems I'm always pushing my Telerik controls to the edge. ;-)

Thanks for the solution. In the future, it would be awesome if I could set a DataSelectedField on the RadAutoCompleteBox and it would automatically add those entries.

0
Chad Hensley
Top achievements
Rank 1
answered on 09 Sep 2012, 05:53 AM
Can you share your code of getting the RACB working with the edit form of a RadGrid?  I have tried in both in an edit template and in a user control and it doesn't work.  

I am using the RACB as a "tags" input per image.  Each image is a record in the grid.

I have opened a support ticket but if you have the answer I would appreciate it.
0
Sypher
Top achievements
Rank 1
answered on 10 Sep 2012, 09:20 PM
Here's what is "working" for me. My only issue now is that since calling (DataView)dsAvailable.Select(args) is a separate call to the database, the proc is being called twice: once to fill in the items in the RACB, and once in the DataBound event to choose the Entries. I would only need to call it once if the RACB had any way to return the current list of DataItems that it uses to fill out the dropdown, but the only list currently available with the RACB is the Entries list. :-P

I may be able to clean it up a bit to use a ControlParameter instead of a plain Parameter, but the ControlParameter wasn't working the first time I tried it, either. And I'm not sure it would work with the second call anyways...

DataSource used by the RACB
<asp:SqlDataSource runat="server" ID="dsAvailable" ConnectionString="<%$ConnectionStrings:FOO %>"
    CancelSelectOnNullParameter="false"
    SelectCommand="dbo.GetPeople" SelectCommandType="StoredProcedure" OnSelecting="dsAvailable_Selecting">
    <SelectParameters>
        ...
        <asp:Parameter Name="ID" ConvertEmptyStringToNull="true" />
    </SelectParameters>
</asp:SqlDataSource>

RACB used in the grid EditFormSettings / FormTemplate
<telerik:RadAutoCompleteBox runat="server" ID="people" Width="550px" Skin="Windows7"
    DropDownWidth="300px" AllowCustomToken="true" InputType="Token"
    OnDataBound="people_DataBound"
    DataSourceID="dsAvailable" DataTextField="FullName" DataValueField="ID">
</telerik:RadAutoCompleteBox>

CodeBehind
protected void dsAvailable_Selecting(object sender, SqlDataSourceCommandEventArgs e)
{
    var id = g1.MasterTableView.DataKeyValues[g1.MasterTableView.DataKeyValues.Count - 1]["ID"];
    if (id != null)
        e.Command.Parameters["@ID"].Value = id;
}
 
protected void people_DataBound(object sender, EventArgs e)
{
    RadAutoCompleteBox people = sender as RadAutoCompleteBox;
    DataSourceSelectArguments args = new DataSourceSelectArguments();
    DataView view = (DataView)dsAvailable.Select(args);
    DataTable table = view.ToTable();
 
    string id = "", name = "", current = "";
    for (int i = 0; i < table.Rows.Count; i++)
    {
        id = table.Rows[i]["ID"].ToString();
        name = table.Rows[i]["FullName"].ToString();
        current = table.Rows[i]["Selected"].ToString();
        if (!string.IsNullOrEmpty(current) && current == "1")
        {
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(id))
            {
                people.Entries.Add(new AutoCompleteBoxEntry(name, id));
            }
        }
    }
}

If there were some way to see the current DataItems in the RACB, I could check that for items that have the Selected flag and then add those items to the Entries list, but I don't see any available object lists from the RACB except the Entries list. So I need to think different...
Tags
AutoCompleteBox
Asked by
Sypher
Top achievements
Rank 1
Answers by
Ivana
Telerik team
Sypher
Top achievements
Rank 1
Chad Hensley
Top achievements
Rank 1
Share this question
or