基本怎么用這里就不啰嗦了,網上有很多文章介紹,包括asp.net那邊也有示例可以下載,這里重點說說Category這個屬性及如何構建webservice,CascadingDropDown得和webservice配合使用才行。先看頁面控件代碼view plaincopy to clipboardPRint?
<!--下拉列表控件-->
<asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList>
<!--對應的CascadingDropDown控件-->
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" LoadingText="加載中" PromptText="請選擇"
ServiceMethod="ClientTypeRootList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlRootClass" Category="RootClientType">
</cc1:CascadingDropDown>
<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" LoadingText="加載中" PromptText="請選擇"
ServiceMethod="ClientTypeSubList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlSubClass" Category="SubClientType" ParentControlID="ddlRootClass">
</cc1:CascadingDropDown>
<!--下拉列表控件-->
<asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList>
<!--對應的CascadingDropDown控件-->
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" LoadingText="加載中" PromptText="請選擇"
ServiceMethod="ClientTypeRootList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlRootClass" Category="RootClientType">
</cc1:CascadingDropDown>
<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" LoadingText="加載中" PromptText="請選擇"
ServiceMethod="ClientTypeSubList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlSubClass" Category="SubClientType" ParentControlID="ddlRootClass">
</cc1:CascadingDropDown> 注意CascadingDropDownr控件中的Category設置,Category主要就是為你CascadingDropDownr控件對應的下拉列表控件選定的值取個名字,好區分是下拉列表的值,所以這個得取的不一樣。ServiceMethod主要就是對應WebSerivce的方法了,指明當前CascadingDropDown控件使用哪個WebSerivce中的方法,其它的么就不細說了。
再來看WebService的代碼view plaincopy to clipboardprint?
/// <summary>
/// ClientType Ajax下拉列表數據服務(注意代碼中的[]是全角,使用的時候替換成半角的)
/// </summary>
[WebService(Namespace = "http:tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService] //<-這段必須得存在
public class ClientTypeCascadingDropDown : System.Web.Services.WebService
{
[WebMethod]
//一級客戶類別相關的WebService方法
public CascadingDropDownNameValue[] ClientTypeRootList(string knownCategoryValues,string category) //<-除了ClientTypeRootList這個方法名可變動,其它不能變動
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-這段也得有,呵呵
//這里就可以放你的數據庫相關代碼,比如把一級客戶類別從數據庫取出來然后存放在一個數組中
//因為這里是一級客戶的下拉列表,所以不用去管那個category的值
//Model.ClientType是我建的一個實體類,其中有ClientTypeName,ClientTypeID,ParentClientTypeID幾個屬性
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//當然你也可以使用DataSet、DataTabel等,在foreach那邊把列表需要的值填充進去就好
//下以部分是下拉列表填充代碼
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
foreach (Model.ClientType model in models)
{
values.Add(new CascadingDropDownNameValue(model.ClientTypeName,model.ClientTypeID.ToString()));
}
return values.ToArray();
}
[WebMethod]
//二級客戶類別相關的WebService方法
public CascadingDropDownNameValue[] ClientTypeSubList(string knownCategoryValues, string category) //<-除了ClientTypeRootList這個方法名可變動,其它不能變動
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-這段也得有,呵呵
//二級客戶的下拉列表,得取得一級的category的值,以下代碼是判斷上級列表的category值,存在或不是空的話把值賦給parentClientTypeID
//RootClientType是一級CascadingDropDown設置的category屬性名稱
int parentClientTypeID;
if (!kv.ContainsKey("RootClientType") || !Int32.TryParse(kv["RootClientType"], out parentClientTypeID))
{
return null;
}
//這里就可以放你的數據庫相關代碼,比如把一級客戶類別從數據庫取出來然后存放在一個數組中
//Model.ClientType是我建的一個實體類,其中有ClientTypeName,ClientTypeID,ParentClientTypeID幾個屬性
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//當然你也可以使用DataSet、DataTabel等,在foreach那邊把列表需要的值填充進去就好
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
foreach (Model.ClientType model in models)
{
values.Add(new CascadingDropDownNameValue(model.ClientTypeName, model.ClientTypeID.ToString()));
}
return values.ToArray();
}
}
/// <summary>
/// ClientType Ajax下拉列表數據服務(注意代碼中的[]是全角,使用的時候替換成半角的)
/// </summary>
[WebService(Namespace = "http:tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService] //<-這段必須得存在
public class ClientTypeCascadingDropDown : System.Web.Services.WebService
{
[WebMethod]
//一級客戶類別相關的WebService方法
public CascadingDropDownNameValue[] ClientTypeRootList(string knownCategoryValues,string category) //<-除了ClientTypeRootList這個方法名可變動,其它不能變動
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-這段也得有,呵呵
//這里就可以放你的數據庫相關代碼,比如把一級客戶類別從數據庫取出來然后存放在一個數組中
//因為這里是一級客戶的下拉列表,所以不用去管那個category的值
//Model.ClientType是我建的一個實體類,其中有ClientTypeName,ClientTypeID,ParentClientTypeID幾個屬性
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//當然你也可以使用DataSet、DataTabel等,在foreach那邊把列表需要的值填充進去就好
//下以部分是下拉列表填充代碼
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
foreach (Model.ClientType model in models)
{
values.Add(new CascadingDropDownNameValue(model.ClientTypeName,model.ClientTypeID.ToString()));
}
return values.ToArray();
}
[WebMethod]
//二級客戶類別相關的WebService方法
public CascadingDropDownNameValue[] ClientTypeSubList(string knownCategoryValues, string category) //<-除了ClientTypeRootList這個方法名可變動,其它不能變動
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-這段也得有,呵呵
//二級客戶的下拉列表,得取得一級的category的值,以下代碼是判斷上級列表的category值,存在或不是空的話把值賦給parentClientTypeID
//RootClientType是一級CascadingDropDown設置的category屬性名稱
int parentClientTypeID;
if (!kv.ContainsKey("RootClientType") || !Int32.TryParse(kv["RootClientType"], out parentClientTypeID))
{
return null;
}
//這里就可以放你的數據庫相關代碼,比如把一級客戶類別從數據庫取出來然后存放在一個數組中
//Model.ClientType是我建的一個實體類,其中有ClientTypeName,ClientTypeID,ParentClientTypeID幾個屬性
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//當然你也可以使用DataSet、DataTabel等,在foreach那邊把列表需要的值填充進去就好
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
foreach (Model.ClientType model in models)
{
values.Add(new CascadingDropDownNameValue(model.ClientTypeName, model.ClientTypeID.ToString()));
}
return values.ToArray();
}
} 基本上一個CascadingDropDown控件就會應對一個Webserivce的方法,如果再有第三個,第四個CascadingDropDown,按ClientTypeSubList為第三個,第四個CascadingDropDown添加對應WebService方法/
出處:http://blog.breakn.net/article.asp?id=389
新聞熱點
疑難解答