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

load combox with service on condition

1 Answer 36 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 29 Jan 2014, 09:24 PM
I am new to using services to load comboboxs.  I wan to load the combobox with attached service on demand in my code behind and not on page load.  how can this be accomplished.  Thank you.

 

<telerik:RadComboBox ID="cbCounty" runat="server" WebServiceSettings-Method="FindCounty" WebServiceSettings-Path="~/AutoComplete.asmx"></telerik:RadComboBox>



1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Jan 2014, 06:36 AM
Hi Kevin,

Please have a look into the sample code snippet which works fine at my end.

ASPX:
<telerik:RadComboBox runat="server" ID="RadComboBox1" Width="300px" EnableLoadOnDemand="true"
    Filter="StartsWith" OnClientItemsRequesting="OnClientItemsRequesting">
    <WebServiceSettings Method="GetProducts" Path="Products.asmx" />
</telerik:RadComboBox>

JavaScript:
<script type="text/javascript">
    function OnClientItemsRequesting(sender, eventArgs) {
        var context = eventArgs.get_context();
        context["FilterString"] = eventArgs.get_text();
    }
</script>

Products.CS C#:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Products : System.Web.Services.WebService {
[WebMethod]
public RadComboBoxItemData[] GetProducts(object context)
{
    IDictionary<string, object> contextDictionary = (IDictionary<string, object>)context;
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    string filterString = ((string)contextDictionary["FilterString"]).ToLower();
    SqlCommand selectCommand = new SqlCommand(
    @" SELECT * FROM Customers WHERE (CompanyName) LIKE '" + filterString + "%'", connection);
    SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
    DataTable products = new DataTable();
    adapter.Fill(products);
    List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(products.Rows.Count);
    foreach (DataRow row in products.Rows)
    {
        RadComboBoxItemData itemData = new RadComboBoxItemData();
        itemData.Text = row["CompanyName"].ToString();
        itemData.Value = row["CompanyName"].ToString();
        result.Add(itemData);
    }
    return result.ToArray();
}
private static DataTable GetChildNodes(string searchString)
{
    SqlCommand selectCommand = new SqlCommand(@"SELECT * FROM [Products] WHERE ProductName LIKE @ProductName + '%'");
    selectCommand.Parameters.AddWithValue("ProductName", searchString.Replace("%", "[%]").Replace("_", "[_]"));
    return GetData(selectCommand);
}
private static DataTable GetData(SqlCommand selectCommand)
{
    selectCommand.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind_newConnectionString"].ConnectionString);
    SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
 
    DataTable data = new DataTable();
    adapter.Fill(data);
 
    return data;
}
}

Thanks,
Princy.
Tags
ComboBox
Asked by
Kevin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or