This is a migrated thread and some comments may be shown as answers.
RadComboBox Highlight Search Text Result
1 Answer 61 Views
This is a migrated thread and some comments may be shown as answers.
Vishal
Top achievements
Rank 1
Vishal asked on 27 Jun 2012, 01:22 PM
Hello Everyone,
Can anyone help me out in highlight (or HTML format) the search string in the combobox results.
I need to use it in the Items_Requested server side method of Combobox
and also a web service method for  combobox in grid.
I tried the following code , and it shows the search text but  created problem for the ecents of Combobox.
//************************  Homepage.aspx.cs **********************************************************  
public static string SetHighlight(string strOrginal, string strNeedle)
    {
        Regex regex = new Regex(strNeedle, RegexOptions.IgnoreCase);
        //return regex.Replace(strOrginal, "<b>" + strNeedle + "</b>");
        return regex.Replace(strOrginal, "<font color=red>" + strNeedle + "</font>");
    }
 
[WebMethod]
    public static RadComboBoxData GetAirportNames(RadComboBoxContext context)
    {
        //DataTable data = GetData(context.Text);
        CommonDL CDL = new CommonDL();
        DataTable data = CDL.LoadAirports_HomePage(context.Text);
        RadComboBoxData comboData = new RadComboBoxData();
        int itemOffset = context.NumberOfItems;
        int endOffset = Math.Min(itemOffset + ItemsPerRequest, data.Rows.Count);
        comboData.EndOfItems = endOffset == data.Rows.Count;
  
        List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(endOffset - itemOffset);
        for (int i = itemOffset; i < endOffset; i++)
        {
            RadComboBoxItemData itemData = new RadComboBoxItemData();
            itemData.Text = SetHighlight(data.Rows[i]["PortName"].ToString(), context.Text.ToString());
            itemData.Value = data.Rows[i]["AirportID"].ToString();
            itemData.Attributes.Add("OrginalText", data.Rows[i]["PortName"].ToString());
            //itemData.Attributes.Add(data.Rows[i]["PortName"].ToString(), context.Text.ToString());
            result.Add(itemData);
        }
        comboData.Message = GetStatusMessage(endOffset, data.Rows.Count);
        comboData.Items = result.ToArray();
        return comboData;
    }
  
  
//************************  Homepage.aspx   **********************************************************  
  
<telerik:RadComboBox Width="160" ID="cmbFromAirportP3" ValidationGroup="P3"
                                                class="text-input"
                                                    AllowCustomText="true"
                                                    runat="server" DataTextField="PortName"
                                                    DataValueField="AirportID"
                                                    EmptyMessage="Select From Airport" Text=""
                                                    EnableLoadOnDemand="true"
                                                    ShowMoreResultsBox="true"
                                   EnableVirtualScrolling="false"
                                   ChangeTextOnKeyBoardNavigation="false"
                                   OnClientDropDownClosing="cmb_Closing"
                                   OnClientItemsRequested="cmbOrgName_ClientItemsRequested"
                                   OnClientItemsRequesting="cmbOrgName_ClientItemsRequesting"
                                   OnClientSelectedIndexChanged="cmbOrgName_ClientSelectedIndexChanged"
                                    OnClientBlur="cmb_Blur"
                                    >
                                                    <WebServiceSettings Method="GetAirportNames" Path="HomePage.aspx" />
                                                </telerik:RadComboBox>
  
  
//************************  Javascript Code  **********************************************************  
<script type="text/javascript" id="S2">
            function cmbOrgName_ClientSelectedIndexChanged(sender, args) {
                //Get OrginalText and set to the selected item
                var orginalText = sender.get_selectedItem().get_attributes().getAttribute("OrginalText");
                sender.set_text(orginalText);
            }
            function cmb_Blur(sender, args) {
                //Get OrginalText and set to the selected item
                  
                var orginalText = sender.get_selectedItem().get_attributes().getAttribute("OrginalText");
                sender.set_text(orginalText);
                sender.hideDropDown();
//                alert('Cmbo lost focus');
            }
            function cmb_Closing(sender, args) {
                //Get OrginalText and set to the selected item
                var orginalText = sender.get_selectedItem().get_attributes().getAttribute("OrginalText");
                sender.set_text(orginalText);
                  
            }
  
              
            function cmbOrgName_ClientItemsRequesting(sender, args) {
                //clear the existing items
                sender.get_items().clear();
            }
            //If no records returned, collapse the dropdown automatically.
  
            function cmbOrgName_ClientItemsRequested(sender, args) {
                if (sender.get_items().get_count() == 0) {
                    sender.toggleDropDown();
                }
            }
              
        </script>

Thanks,
Vishal

1 Answer, 1 is accepted

Sort by
0
Accepted
Abhi Shinde
Top achievements
Rank 1
answered on 18 Aug 2012, 07:12 AM
Asked by
Vishal
Top achievements
Rank 1
Answers by
Abhi Shinde
Top achievements
Rank 1
Share this question
or