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

SearchContext with 2 differents datasource

4 Answers 157 Views
SearchBox
This is a migrated thread and some comments may be shown as answers.
Riad
Top achievements
Rank 1
Riad asked on 03 Feb 2015, 07:21 PM
Hi guys, I've been struggling with a problem all day.

The scenario I want to acheive is:
With the same radsearchbox, I wanna search in 2 differents datasource.

Let me explain. On my main page, if I choose the "News" context, I wanna be able to search in my News database on 3 differents fields.
I was able to get the expected result with this code.

<telerik:RadSearchBox ID="rsbSearch" runat="server" InputType="Text" TextSettings-SelectionMode="Single" DataSourceID="SqlSearchDataSource"
                                    DataValueField="ID" OnDataSourceSelect="rsbSearch_DataSourceSelect" EmptyMessage="Rechercher" Width="250px" MaxResultCount="5" >
                                    <SearchContext ShowDefaultItem="false" DataKeyField="Titre" >
                                        <Items>
                                            <telerik:SearchContextItem Text="Nouvelle" Key="News" />
                                            <telerik:SearchContextItem Text="Bottin" Key="Bottin" />
                                        </Items>
                                    </SearchContext>
                                     
 
                                    <DropDownSettings CssClass="rsbSearchPopUp">
                                        <ItemTemplate>
                                            <asp:PlaceHolder ID="test1" runat="server">
                                                <table class="tableNews" style="z-index:200;" >
                                                    <tbody>
                                                        <tr>
                                                            <td class="actualityDescription">
                                                                <table>
                                                                    <tr>
                                                                        <td>.............

and the code behind
protected void rsbSearch_DataSourceSelect(object sender, SearchBoxDataSourceSelectEventArgs e)
        {
            SqlDataSource source = (SqlDataSource)e.DataSource;
            RadSearchBox mySearchBox = (RadSearchBox)sender;
 
            string filterString = e.FilterString;
 
            if (e.SelectedContextItem != null && e.SelectedContextItem.Key.ToLower() == "news")
            {
 
                source.SelectCommand = "SELECT TOP 100 * FROM [NewsData] WHERE DescriptionCourte Like '%" + filterString + "%'" +
                    " OR Titre Like '%" + filterString + "%'" +
                    " OR DescriptionLongue Like '%" + filterString + "%'";
            }
            else {
                //mySearchBox.DataSource = Intranet.BL.Employe.GetEmployees(true, filterString);
            }
        }


Now by choosing the "Bottin" context, I wanna be able to get the result from a business logic object named Employe from a function GetEmployeList(string name). I tried to set the new datasource of my searchbox with the result of my function, but it didn't work since i already define a datasourceID

Any hints on how I could be able to have 2 different datasource depending on the context choice?

Thank you.

4 Answers, 1 is accepted

Sort by
0
Riad
Top achievements
Rank 1
answered on 03 Feb 2015, 07:24 PM
I wanna be able to choose a different ItemTemplate too according to the choosen context, however, I don't have any ItemBound event that i could attach....

0
Nencho
Telerik team
answered on 06 Feb 2015, 03:57 PM
Hello Simon,

i am afraid that you cannot change the DataSource control dynamically. However, I would suggest you to use a StoredProcedure to supply the SearchBox with data. Thus, you can pass the selected item from the
SearchContext as a parameter for the procedure and return the data from the corresponding table.

As for the change of the template - I am afraid that currently this is not applicable, since there is no event to handle the change of the selection in the ContextMenu. However, I have already forwarded this to our developer team, as a feature request. In addition, I have updated your Telerik Points for pointing that out.

Regards,
Nencho
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Riad
Top achievements
Rank 1
answered on 06 Feb 2015, 04:02 PM
Thank you, I'll use a stored proc. 

As for the Itemtemplate, I found a work around for the moment.

Many thanks.

Simon
0
roy
Top achievements
Rank 1
answered on 12 Jun 2016, 10:05 AM

Hey Simon i know this is an old post but do you think you can post the work around you found for dynamic templates?

 

Thanks!

Tags
SearchBox
Asked by
Riad
Top achievements
Rank 1
Answers by
Riad
Top achievements
Rank 1
Nencho
Telerik team
roy
Top achievements
Rank 1
Share this question
or