This is a migrated thread and some comments may be shown as answers.
Add Web Service to Dynamically Created ComboBox
1 Answer 9 Views
This is a migrated thread and some comments may be shown as answers.
Beryl
Top achievements
Rank 1
Beryl asked on 08 Jan 2018, 03:08 PM

I have to create RadComboBoxes dynamically in a Repeater.  I'm trying to find a way to set the WebServiceSettings in the code behind.  I found this code that uses JQuery:

function clientLoad(sender) {
    sender.set_webServiceSettings('{"path":"Products.asmx","method":"GetCompanyNames"}');
}

But the service does not return Json so I receive an error.  I create the control in the code behind like this:

            List <ModuleView.JobBO> list = new List<ModuleView.JobBO>();
            switch (moduledatakey)
            {
                case "JOB":
                    list = JobDA.Job_GetList_ByJobNumberContains(DB_Context, mfilter, top, companyId);
                    RadComboBox ddl = new RadComboBox();
                    ddl.ID = "ddlJob";
                    ddl.ItemDataBound += new RadComboBoxItemEventHandler(ddlJob_ItemDataBound);
                    ddl.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(ddlJob_ItemsRequested);
                    ddl.EmptyMessage = "Type to Select...";
                    ddl.EnableLoadOnDemand = true;
                    ddl.EnableTextSelection = true;
                    ddl.DataSource = list;
                    ddl.Text = "";
                    ddl.DataValueField = "JobId";
                    ddl.DataBind();
                    ddl.OnClientLoad = "clientLoad";
                    ddl.DropDownAutoWidth = Telerik.Web.UI.RadComboBoxDropDownAutoWidth.Enabled;
                    ddl.AutoPostBack = true;
                    ddl.EnableViewState = false;
                    ddl.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(qfTicket_cmbJob_SelectedIndexChanged);
                    plc2.Controls.Add(ddl);
                    break;
            }

and use the following to implement the "LoadOnDemand":

        protected void ddlJob_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            string mfilter = e.Text;
            int top = 50;
            Guid? companyId = null;

            List<ModuleView.JobBO> list = new List<ModuleView.JobBO>();
            list = JobDA.Job_GetList_ByJobNumberContains(DB_Context, mfilter, top, companyId);
            ((RadComboBox)sender).DataSource = list;
            ((RadComboBox)sender).DataBind();

        }

This works fine accept the LoadOnDemand is EXTREMELY slow.   I want to use the following web method which we are using on other comboboxes that are NOT created dynamically.

 [WebMethod(EnableSession = true)]
        public RadComboBoxData GetJobs(RadComboBoxContext context)
        {
            //get data
            List<ModuleView.JobBO> lComps = new List<ModuleView.JobBO>();
            COG_ContextDB db_context = UtilityBA.GetAspNetDataContext();
            //company id?
            App_Code_COGS.UI.UIUtilities.GridState oState = App_Code_COGS.UI.UIUtilities.GetGridStateFromSession();
            Guid? gCustomerId = (UtilityBA.IsValidGuid(oState.CustomerId) ? (Guid?)Guid.Parse(oState.CustomerId) : null);
            //get data
            lComps = JobBA.Job_GetList_ByJobNumberContains(db_context, context.Text, 50, gCustomerId);
            if (lComps == null)
            {
                lComps = new List<ModuleView.JobBO>();
            }
            //prep data controls
            RadComboBoxData comboData = new RadComboBoxData();
            List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(context.NumberOfItems);
            //get top N
            try
            {
                int itemsPerRequest = 10;
                int itemOffset = context.NumberOfItems;
                int endOffset = itemOffset + itemsPerRequest;
                if (endOffset > lComps.Count)
                {
                    endOffset = lComps.Count;
                }
                if (endOffset == lComps.Count)
                {
                    comboData.EndOfItems = true;
                }
                else
                {
                    comboData.EndOfItems = false;
                }
                result = new List<RadComboBoxItemData>(endOffset - itemOffset);
                for (int i = itemOffset; i < endOffset; i++)
                {
                    RadComboBoxItemData itemData = new RadComboBoxItemData();
                    itemData.Text = ((ModuleView.JobBO)lComps[i]).JobNumber.ToString() + " - " + ((ModuleView.JobBO)lComps[i]).JobName.ToString();
                    itemData.Value = ((ModuleView.JobBO)lComps[i]).JobId.ToString();
                    result.Add(itemData);
                }
                if (lComps.Count > 0)
                {
                    comboData.Message = String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset.ToString(), lComps.Count.ToString());
                }
                else
                {
                    comboData.Message = "No matches";
                }
            }
            catch (Exception e)
            {
                comboData.Message = e.Message;
            }
            var json = JsonConvert.SerializeObject(result);

            comboData.Items = result.ToArray();

            return comboData;
        }

 

Is there any way to assign the WebServiceSettings in the codebehind?

 

 

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Peter Milchev
Telerik team
answered on 05 Feb 2018, 02:14 PM
Hello Beryl,

You can create and assign the properties of the ComboBoxes in the ItemCreated event of the Repeater as demonstrated in the attached project. To run the project, you should add the .NET 4.5 version of the Telerik assemblies in the bin folder.

Default.aspx
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated">
    <ItemTemplate>
        <asp:PlaceHolder ID="myPlaceHolder1" runat="server"></asp:PlaceHolder>
        
    </ItemTemplate>
</asp:Repeater>
<hr />
<asp:Repeater ID="Repeater2" runat="server" OnItemCreated="Repeater2_ItemCreated">
    <ItemTemplate>
        <asp:PlaceHolder ID="myPlaceHolder1" runat="server"></asp:PlaceHolder>
        
    </ItemTemplate>
</asp:Repeater>

Default.apsx.cs

protected void Page_Init(object sender, EventArgs e)
{
    Repeater1.DataSource = new string[] { "1", "2", "3", "4" };
    Repeater1.DataBind();
    Repeater2.DataSource = new string[] { "1", "2", "3", "4" };
    Repeater2.DataBind();
}
 
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    var placeHolder = e.Item.FindControl("myPlaceHolder1") as PlaceHolder;
 
    var combo = new RadComboBox();
    combo.ID = "RadComboBoxRegion";
    combo.RenderMode = RenderMode.Lightweight;
    combo.MinFilterLength = 1;
    combo.Height = 200;
    combo.Width = 315;
    combo.EnableLoadOnDemand = true;
    combo.EmptyMessage = "Choose a Region";
    combo.WebServiceSettings.Path = "Territories.asmx";
    combo.WebServiceSettings.Method = "GetTerritories";
 
    placeHolder.Controls.Add(combo);
}
 
protected void Repeater2_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    var placeHolder = e.Item.FindControl("myPlaceHolder1") as PlaceHolder;
 
    var combo = new RadComboBox();
    combo.ID = "RadComboBoxProduct";
    combo.RenderMode = RenderMode.Lightweight;
    combo.MinFilterLength = 1;
    combo.Height = 200;
    combo.Width = 315;
    combo.EnableLoadOnDemand = true;
    combo.EmptyMessage = "Choose a Product";
    combo.ItemsRequested += RadComboBoxProduct_ItemsRequested;
 
    placeHolder.Controls.Add(combo);
}
 
protected void RadComboBoxProduct_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
    string sqlSelectCommand = "SELECT [ProductID], [ProductName], [UnitPrice], [UnitsInStock] from [Products] WHERE [ProductName] LIKE @text + '%' ORDER BY [ProductName]";
 
    SqlDataAdapter adapter = new SqlDataAdapter(sqlSelectCommand,
        ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    adapter.SelectCommand.Parameters.AddWithValue("@text", e.Text);
    DataTable dataTable = new DataTable();
    adapter.Fill(dataTable);
 
    foreach (DataRow dataRow in dataTable.Rows)
    {
        RadComboBoxItem item = new RadComboBoxItem();
 
        item.Text = (string)dataRow["ProductName"];
        item.Value = dataRow["ProductID"].ToString();
 
        decimal unitPrice = (decimal)dataRow["UnitPrice"];
        short unitsInStock = (short)dataRow["UnitsInStock"];
 
        item.Attributes.Add("UnitPrice", unitPrice.ToString());
        item.Attributes.Add("UnitsInStock", unitsInStock.ToString());
 
        item.Value += ":" + unitPrice;
 
        (sender as RadComboBox).Items.Add(item);
 
        item.DataBind();
    }
}


Territories.asmx
<%@ WebService Language="C#" CodeBehind="~/App_Code/Territories.cs" Class="Territories" %>

Territories.cs
[ScriptService]
public class Territories : WebService
{
    [WebMethod]
    public RadComboBoxItemData[] GetTerritories(RadComboBoxContext context)
    {
        SqlConnection connection =
            new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
 
        SqlCommand selectCommand =
            new SqlCommand(@"    SELECT [TerritoryID], [TerritoryDescription] FROM [Territories]
                                 WHERE LOWER ([TerritoryDescription]) LIKE @text + '%' ORDER BY [TerritoryDescription]", connection);
 
        SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
        adapter.SelectCommand.Parameters.AddWithValue("@text", context.Text.ToLower());
        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["TerritoryDescription"].ToString();
            itemData.Value = row["TerritoryID"].ToString();
 
            result.Add(itemData);
        }
 
        return result.ToArray();
    }
}



Regards,
Peter Milchev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Asked by
Beryl
Top achievements
Rank 1
Answers by
Peter Milchev
Telerik team
Share this question
or