load combox with service on condition

2 posts, 0 answers
  1. Kevin
    Kevin avatar
    265 posts
    Member since:
    Jun 2011

    Posted 29 Jan 2014 Link to this post

    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>



  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 30 Jan 2014 in reply to Kevin Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top