This example shows how the AjaxControlToolkit CascadingDropDownExtender control can be used with .netTiers. This is a very simple example, but shows how to use this very nice control.
First, here is the database schema:
[imageauto|Database Schema|{UP}DocumentationImages%2fdbschema.png]
The following code snippet shows the ASPX page. Again, it's very simple containing two drop down lists. When an item is selected from the first list, the items in the second list are changed accordingly.
1
<asp:ScriptManager ID="scriptmanager1" EnablePartialRendering="true" runat="Server" />
2
<div>
3
<asp:DropDownList runat="server" id="dataLevel1" AppendDataBoundItems="true"
4
EnableViewState="true" >
5
<asp:ListItem Text="--All--" Value="" />
6
</asp:DropDownList>
7
8
<ajaxToolkit:CascadingDropDown
9
ID="ccdLevel1"
10
runat="server"
11
TargetControlID="dataLevel1"
12
Category="Level1"
13
PromptText="--All--"
14
15
ServiceMethod="GetLevel1Data" />
16
17
<br />
18
19
20
<asp:DropDownList runat="server" id="dataLevel2" AppendDataBoundItems="true"
21
DataValueField="Level2Id"
22
DataTextField="Level2Name"
23
EnableViewState="true">
24
<asp:ListItem Text="--All--" Value="" />
25
</asp:DropDownList>
26
27
<ajaxToolkit:CascadingDropDown
28
ID="ccdLevel2"
29
runat="server"
30
TargetControlID="dataLevel2"
31
ParentControlID="dataLevel1"
32
Category="Level2"
33
PromptText="--All--"
34
ServiceMethod="GetLevel2Data" LoadingText="Loading List..." />
35
</div>
36
</form>
The last part to get this all working is the code-behind for the ASPX page show below. Note that the web page has the ScriptService attribute attached to it, and the methods that get called by the CascadingDropDown control have the WebMethod attribute.
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
using NetTiersTest.Entities;
13
using NetTiersTest.Services;
14
using AjaxControlToolkit;
15
using System.Web.Services;
16
using System.Collections.Specialized;
17
using System.Collections.Generic;
18
19
[System.Web.Script.Services.ScriptService()]
20
public partial class CascadingDropDownSample : System.Web.UI.Page
21
...{
22
protected void Page_Load(object sender, EventArgs e)
23
...{
24
25
}
26
27
28
[WebMethod]
29
public static AjaxControlToolkit.CascadingDropDownNameValue[] GetLevel1Data(string knownCategoryValues,
30
string category)
31
...{
32
33
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
34
35
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
36
37
CDD_Level1Service service = new CDD_Level1Service();
38
TList<CDD_Level1> list = service.GetAll();
39
foreach (CDD_Level1 entity in list)
40
...{
41
CascadingDropDownNameValue val = new CascadingDropDownNameValue(entity.Level1Name, entity.Level1Id.ToString());
42
values.Add(val);
43
}
44
45
return values.ToArray();
46
}
47
48
[WebMethod]
49
public static AjaxControlToolkit.CascadingDropDownNameValue[] GetLevel2Data(string knownCategoryValues,
50
string category)
51
...{
52
53
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
54
55
Guid level1Id;
56
57
if (!kv.ContainsKey("Level1")) return null;
58
59
level1Id = new Guid(kv["Level1"].ToString());
60
61
CDD_Level2Service service = new CDD_Level2Service();
62
TList<CDD_Level2> list = service.GetByLevel1Id(level1Id);
63
64
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
65
66
foreach (CDD_Level2 entity in list)
67
...{
68
CascadingDropDownNameValue val = new CascadingDropDownNameValue(entity.Level2Name, entity.Level2Id.ToString());
69
values.Add(val);
70
}
71
72
return values.ToArray();
73
}
74
75
76
}
77