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

What is the proper way to implement search in one column in RadComboBox and display text in another column? (Is there a DataSearchField other than a DataTextField and DataValueField?)

2 Answers 109 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Joey
Top achievements
Rank 1
Joey asked on 03 Jul 2013, 04:08 AM
Hi guys, good day to you.

I've implemented an RadComboBox with multiple columns in an item template and using automatic load on demand on a very large dataset, and it is working very well.
However i have some questions I hope you can help me.

1) The combobox is implemented using Filer="Contains", and it searches across all the columns in the item template using ItemsPerRequest = "10". In order to achieve this, I've created a column in the datasource called "SearchColumn", which is a string consisting of "col1" + "col2"+"col3" etc.... The DataTextField is then set to DataTextField = "SearchColumn". This works very well and implements the search functionality that I want. But because the DataTextField is now this SearchColumn, whenever the user clicks an item, the text in the RadComboBox becomes this very long string. For example, if I have a row in the dropdown with columns - "Vehicle0001" "Car" "Blue" "4 wheels",my SearchColumn becomes "Vehicle0001CarBlue4Wheels", and if the user searches any of these columns the row will show up. Clicking on the item will set the text in the RadComboBox to "Vehicle0001CarBlue4Wheels". But this is not what I want to display as the text, so I have added a viewstate variable to store and display the selected value,

    RadComboBox_SelectedIndexChanged(Object sender, EventArgs e)
    {
      ViewState["SelectedVehicle"] = e.SelectedValue
    }

    and added a line in Page_PreRender to always set the text to the value to display.

    protected void Page_PreRender(object sender, EventArgs e)
    {
      RadComboBox.Text = ViewState["SelectedVehicle"]
    }

Is this the recommended way to implement a search across all columns and yet display a different text on selection? I am wondering if there is a property to set in the RadComboBox control to indicate which is the datafield to search ("SearchColumn"), or does it only search the DataTextField?

2) This method is working very well for me, but there is a small problem. If the user click the RadComboBox and expands the drop down, but does not change anything, it will auto set the value to the DataTextField ("Vehicle0001CarBlue4Wheels") and not "Vehicle0001" since there is no postback. What is the recommended way to resolve this?

3) I am hoping to replace the Page_Prerender server side setting the ViewState variable, with a client side set text setting after every SelectedIndexChanged. So I have tried something like this, changing the SearchColumn to be something like Col1+ '|' + Col2 .... so "Vehicle0001CarBlue4Wheels" becomes "Vehicle0001|Car|Blue|4Wheels", then I added OnClientSelectedIndexChanged function following the demo

    function RadComboBoxOnClientSelectedIndexChanged(sender, eventArgs) {
                var item = eventArgs.get_item();
                sender.set_text(item.get_text().split("|")[0]);
    }

But there is a problem, the RadComboBoxOnClientSelectedIndexChanged is called before server processes the server side function RadComboBox_SelectedIndexChanged. By calling sender.set_text(); on a custom value, the SelectedValue becomes "", it will never select the correct item. How should I resolve this? Is there a way to call set_text after server processes RadComboBox_SelectedIndexChanged?

Thanks for reading.

2 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 05 Jul 2013, 12:28 PM
Hello Joey,

I would suggest you a slightly different approach. You could handle the OnItemsRequested and perform the filtering at server side. Thus you could use the e.Text (typed in the input characters) and request a datasource, which would match/contains in the SearchColumn in your database. You could loop through the returned datasource (dataTable.Rows) and create the new RadComboBoxItem. Similar approach is implemented in this online demo. Thus you would eliminate the need of managing the input Text of the RadComboBox and if the user selects an item, the DataTextField property would be displayed in the input.

Regards,
Nencho
Telerik
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 the blog feed now.
0
Joey
Top achievements
Rank 1
answered on 10 Aug 2013, 12:31 PM
Thanks Nencho, it works very well
Tags
ComboBox
Asked by
Joey
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Joey
Top achievements
Rank 1
Share this question
or