MVC
Web forms
Web forms Tutorials

AjaxDropdown

You can create it like below, this control requires an Url from where it is going to get it's data via ajax

<o:AjaxDropdown runat="server" ID="Fruitys" Url="~/svc/aja.svc/CategoriesDropdown"></o:AjaxDropdown>      

the service method looks like this:

[WebGet]
[OperationContract]
public IEnumerable<SelectListItem> CategoriesDropdown(string v)
{
    return Categories.Select(o => new SelectListItem { Value = o.Id, Text = o.Name, Selected = v == o.Id.ToString() });
}

The v parameter is the current value of the dropdown, and the method returns a collection of SelectListItem which has 3 properties: Value, Text and Selected. So basically in the service method you get the selected value of the dropdown and you need to return a collection of options for the dropdown, and the item that was selected you mark it with Selected = true, the rest are false.

Binding to one parent using ParentId

The Ajaxdropdown can be bound to a parent, and when the parent value will be changed, the child will also change it's value based on the parent's new value. Here's an example:

<o:AjaxDropdown runat="server" ID="Categories" Url="~/svc/aja.svc/CategoriesDropdown"></o:AjaxDropdown> = 
<%
    Fruits.ParentId = Categories.ClientID;
%>
<o:AjaxDropdown runat="server" ID="Fruits" Url="~/svc/aja.svc/FruitsDropdown"></o:AjaxDropdown>

This way the svc/aja.svc/FruitsDropdown method besides the v will also receive another parameter parent which will be the value of the parent.

[WebGet]
[OperationContract]
public IEnumerable<SelectListItem> FruitsDropdown(string v, string parent)
{
    var list = new List<SelectListItem> { new SelectListItem { Text = "not selected", Value = "" } };
    list.AddRange(Fruits.Where(f => f.Category.Id.ToString() == parent)
    .Select(o => new SelectListItem
    {
        Text = o.Name,
        Value = o.Id,
        Selected = v == o.Id.ToString()
    }));
    return list;
}

Binding to many parents using Data

To bind the AjaxDropdown to many parents use Data property which is a Dictionary, so for each KeyValuePair of the Data Dictionary the Key will be the name of the parameter sent to the servcie method, and the Value will be the ClientID of the parent, here's an example:

<o:AjaxDropdown runat="server" ID="Cat1" Url="~/svc/aja.svc/CategoriesDropdown" Value="1">
</o:AjaxDropdown> and 
<o:AjaxDropdown runat="server" ID="Cat2" Url="~/svc/aja.svc/CategoriesDropdown" Value="4">
</o:AjaxDropdown>    =
    <%
    FruitsFrom2Cat.Data = new Dictionary<string, string>
                        {
                            {"c1",Cat1.ClientID},
                            {"c2",Cat2.ClientID}
                        };
%>
<o:AjaxDropdown runat="server" ID="FruitsFrom2Cat" Url="~/svc/aja.svc/FruitsFrom2CatDropdown"></o:AjaxDropdown>

This way the service method besides the v will also receive c1 and c2 with the values of Cat1 and Cat2 controls.

[WebGet]
[OperationContract]
public IEnumerable<SelectListItem> FruitsFrom2CatDropdown(string v, string c1, string c2)
{
    var list = new List<SelectListItem> { new SelectListItem { Text = "not selected", Value = "" } };

    list.AddRange(Fruits.Where(f => f.Category.Id.ToString() == c1 || f.Category.Id.ToString() == c2)
        .Select(o => new SelectListItem
        {
            Text = o.Name,
            Value = o.Id,
            Selected = v == o.Id.ToString()
        }));
    return list;
}    

Sending predefined values to service method using Parameters

Use this when you need to send additional parameters with values to the service method, example:

<%
FruitWithPars.Parameters = new Dictionary<string, string>{ {"category","3"}, {"hi","foo"}, {"ho", "bar"} };
%>
<o:AjaxDropdown runat="server" ID="FruitWithPars" Url="~/svc/aja.svc/FruitsWithParsDropdown"></o:AjaxDropdown> 

Now the service method will get 3 additional parameters: category, hi and ho

[WebGet]
[OperationContract]
public IEnumerable<SelectListItem> FruitsWithParsDropdown(string v, string category, string hi, string ho)
{
    //category = "3"
    //hi = "foo"
    //ho = "bar"
...
}

Binding to a parent with multiple values

When you bind to a MultiLookup or an AjaxCheckboxList, in the service method you will receive a string representation of a JSON Array, example:

public IEnumerable<SelectListItem> FruitsFromMultiDropdown(string v, string parent)
{
    //e.g.
    //parent = "[1,3,9]";
    int[] ids = parent.GetIntArray(); // GetIntArray is an extension from Omu.AwesomeWebForms.Core
    ...
}