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

RadSearchBox and DataKeyNames not working

1 Answer 192 Views
SearchBox
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 19 Feb 2018, 01:45 AM

The documentation says I can include more than the one column in the search of my DataTable by using the DataKeyNames property. Maybe I am not understanding but its not working. I have my DataTextField column set to "Name" and my DataKeyNames set to "Alias". I expected the search to go against the Name column and the Alias column but it seems to be only searching the Name column. 

Does not get any simpler scenario than this so I must not be understanding how DataKeyNames works.

DataTable columns: Id, Name, Alias

<telerik:RadSearchBox ID="requestSearchBox" runat="server" Width="400" CssClass="SearchBox" EnableAutoComplete="true"
                            DataValueField ="Id"
                            DataKeyNames= "Alias"
                            DataTextField="Name">
                        </telerik:RadSearchBox>

 

Brian

Brian
Top achievements
Rank 1
commented on 19 Feb 2018, 06:41 PM

Well, seems DataKeyNames are what is returned, not what is searched.

So I guess not what I wanted but I did need this as well.

Brian

1 Answer, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 21 Feb 2018, 04:51 PM
Hi Brian,

Yes, the DataKeyNames property is used for the search result values, you can find details about its funtionality here:
https://docs.telerik.com/devtools/aspnet-ajax/controls/searchbox/functionality/datakeynames

If you want to search in multiple columns, you should pass all of them to the DataKeyNames property, like in this demo:
DataKeyNames="sport,birthday,country"


Regards,
Vessy
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Thorsten
Top achievements
Rank 1
commented on 27 Feb 2025, 06:37 PM

We have the same issue.

I think what Brian means is that he want to show the Name in the AutoCompleteBox also if it isn´t in the column Name but in the column Alias.

We need the same.

We have a column for item (product) search keywords in a DataTable but we want to show the item (product) description inside the autocomplete box. There can be keyords, e.g. item no etc., that are not include the item description.

 

e.g.

Keywords: intel i7, 15.3", hdmi, usb-3, lenovo, thinkpad, 1887, .....

Item-Description: Lenovo Thinkpad X1 Carbon

Type inside SearchBox "intel i7" and expected result in AutoCompleteBox:

Lenovo Thinkpad X1 Carbon

 

 

Attila Antal
Telerik team
commented on 04 Mar 2025, 02:29 PM

Hi Thorsten,

For clarification, the fields set in the DataKeyNames property are not used for searching. The Search happens on the field defined in the DataTextField, however, by default the items that are returned only hold the Text (DataTextField) and Value (DataValueField). If you wish to have access to more than one column of data, you can add those fields to the DataKeyNames property. So when an item is returned in the search result, will also contain the information for the other columns (set in the DataKeyNames property) and can be used when searching.

For example, in the OnClientSearch event, the args.get_dataItem() will contain a dataItem object with the additional fields

SearchBox markup

<telerik:RadSearchBox ID="RadSearchBox1" runat="server" DataTextField="ShipName" DataValueField="OrderID" DataKeyNames="OrderDate,Freight" OnClientSearch="OnClientSearch">
</telerik:RadSearchBox>

Text, Value and additional info in the DataItem (DataKeyNames fields) object

To answer your question, you can capture the callback parameters of the SearchBox (as soon as it makes the request to server) and use it to filter the data source on any column you wish. Then you can return the filtered results. 

In this example, I am using the keyword to search against the ShipName column, despite having the ShipName set as the DataTextField. The results display the ShipName, by searching by ShipCountry. Note: Using this approach, you can filter by multiple columns as well.

class ComboSearchData
{
    public string Text { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.Request.Form["__CALLBACKID"] != null && Page.Request.Form["__CALLBACKID"] == RadSearchBox1.ID)
    {
        string callBackParam = Page.Request.Form["__CALLBACKPARAM"];

        ComboSearchData data = new JavaScriptSerializer().Deserialize<ComboSearchData>(callBackParam);

        string text = data.Text;

        // Use the text to filter by any column of the data source and return the results to 

        DataTable filteredData = OrdersTable().Select(string.Format("ShipCountry LIKE '%{0}%'", text)).CopyToDataTable();

        RadSearchBox1.DataSource = filteredData;
    }
    else
    {
        RadSearchBox1.DataSource = OrdersTable();
    }

    RadSearchBox1.DataBind();
}

I hope this will clarify the confusion and help you implement the desired behavior.

Tags
SearchBox
Asked by
Brian
Top achievements
Rank 1
Answers by
Vessy
Telerik team
Share this question
or