本文为【asp.net AJAX中的CascadingDropDown控件使用心得】的汉字拼音对照版显示拼音
基ji本ben怎zen么me用yong这zhe里li就jiu不bu啰luo嗦suo了le,网wang上shang有you很hen多duo文wen章zhang介jie绍shao,包bao括kuoasp.net那na边bian也ye有you示shi例li可ke以yi下xia载zai,这zhe里li重chong点dian说shuo说shuoCategory这zhe个ge属shu性xing及ji如ru何he构gou建jianwebservice,CascadingDropDown得de和hewebservice配pei合he使shi用yong才cai行xing。先xian看kan页ye面mian控kong件jian代dai码maview plaincopy to clipboardprint?
<!--下xia拉la列lie表biao控kong件jian-->
<asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList>&nbsp;
<asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList>&nbsp;
<!--对dui应ying的deCascadingDropDown控kong件jian-->
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" LoadingText="加jia载zai中zhong" PromptText="请qing选xuan择ze"
ServiceMethod="ClientTypeRootList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlRootClass" Category="RootClientType">
</cc1:CascadingDropDown>
<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" LoadingText="加jia载zai中zhong" PromptText="请qing选xuan择ze"
ServiceMethod="ClientTypeSubList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlSubClass" Category="SubClientType" ParentControlID="ddlRootClass">
</cc1:CascadingDropDown>
<!--下xia拉la列lie表biao控kong件jian-->
<asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList>&nbsp;
<asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList>&nbsp;
<!--对dui应ying的deCascadingDropDown控kong件jian-->
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" LoadingText="加jia载zai中zhong" PromptText="请qing选xuan择ze"
ServiceMethod="ClientTypeRootList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlRootClass" Category="RootClientType">
</cc1:CascadingDropDown>
<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" LoadingText="加jia载zai中zhong" PromptText="请qing选xuan择ze"
ServiceMethod="ClientTypeSubList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlSubClass" Category="SubClientType" ParentControlID="ddlRootClass">
</cc1:CascadingDropDown>注zhu意yiCascadingDropDownr控kong件jian中zhong的deCategory设she置zhi,Category主zhu要yao就jiu是shi为wei你niCascadingDropDownr控kong件jian对dui应ying的de下xia拉la列lie表biao控kong件jian选xuan定ding的de值zhi取qu个ge名ming字zi,好hao区qu分fen是shi下xia拉la列lie表biao的de值zhi,所suo以yi这zhe个ge得de取qu的de不bu一yi样yang。ServiceMethod主zhu要yao就jiu是shi对dui应yingWebSerivce的de方fang法fa了le,指zhi明ming当dang前qianCascadingDropDown控kong件jian使shi用yong哪na个geWebSerivce中zhong的de方fang法fa,其qi它ta的de么me就jiu不bu细xi说shuo了le。
再zai来lai看kanWebService的de代dai码maview plaincopy to clipboardprint?
/// <summary>
/// ClientType Ajax下xia拉la列lie表biao数shu据ju服fu务wu(注zhu意yi代dai码ma中zhong的de[]是shi全quan角jiao,使shi用yong的de时shi候hou替ti换huan成cheng半ban角jiao的de)
/// </summary>
[WebService(Namespace = "http:tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService] //<-这zhe段duan必bi须xu得de存cun在zai
public class ClientTypeCascadingDropDown : System.Web.Services.WebService
{
[WebMethod]
//一yi级ji客ke户hu类lei别bie相xiang关guan的deWebService方fang法fa
public CascadingDropDownNameValue[] ClientTypeRootList(string knownCategoryValues,string category) //<-除chu了leClientTypeRootList这zhe个ge方fang法fa名ming可ke变bian动dong,其qi它ta不bu能neng变bian动dong
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-这zhe段duan也ye得de有you,呵he呵he
//这zhe里li就jiu可ke以yi放fang你ni的de数shu据ju库ku相xiang关guan代dai码ma,比bi如ru把ba一yi级ji客ke户hu类lei别bie从cong数shu据ju库ku取qu出chu来lai然ran后hou存cun放fang在zai一yi个ge数shu组zu中zhong
//因yin为wei这zhe里li是shi一yi级ji客ke户hu的de下xia拉la列lie表biao,所suo以yi不bu用yong去qu管guan那na个gecategory的de值zhi
//Model.ClientType是shi我wo建jian的de一yi个ge实shi体ti类lei,其qi中zhong有youClientTypeName,ClientTypeID,ParentClientTypeID几ji个ge属shu性xing
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//当dang然ran你ni也ye可ke以yi使shi用yongDataSet、DataTabel等deng,在zaiforeach那na边bian把ba列lie表biao需xu要yao的de值zhi填tian充chong进jin去qu就jiu好hao
//下xia以yi部bu分fen是shi下xia拉la列lie表biao填tian充chong代dai码ma
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]
//二er级ji客ke户hu类lei别bie相xiang关guan的deWebService方fang法fa
public CascadingDropDownNameValue[] ClientTypeSubList(string knownCategoryValues, string category) //<-除chu了leClientTypeRootList这zhe个ge方fang法fa名ming可ke变bian动dong,其qi它ta不bu能neng变bian动dong
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-这zhe段duan也ye得de有you,呵he呵he
//二er级ji客ke户hu的de下xia拉la列lie表biao,得de取qu得de一yi级ji的decategory的de值zhi,以yi下xia代dai码ma是shi判pan断duan上shang级ji列lie表biao的decategory值zhi,存cun在zai或huo不bu是shi空kong的de话hua把ba值zhi赋fu给geiparentClientTypeID
//RootClientType是shi一yi级jiCascadingDropDown设she置zhi的decategory属shu性xing名ming称cheng
int parentClientTypeID;
if (!kv.ContainsKey("RootClientType") || !Int32.TryParse(kv["RootClientType"], out parentClientTypeID))
{
return null;
}
//这zhe里li就jiu可ke以yi放fang你ni的de数shu据ju库ku相xiang关guan代dai码ma,比bi如ru把ba一yi级ji客ke户hu类lei别bie从cong数shu据ju库ku取qu出chu来lai然ran后hou存cun放fang在zai一yi个ge数shu组zu中zhong
//Model.ClientType是shi我wo建jian的de一yi个ge实shi体ti类lei,其qi中zhong有youClientTypeName,ClientTypeID,ParentClientTypeID几ji个ge属shu性xing
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//当dang然ran你ni也ye可ke以yi使shi用yongDataSet、DataTabel等deng,在zaiforeach那na边bian把ba列lie表biao需xu要yao的de值zhi填tian充chong进jin去qu就jiu好hao
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下xia拉la列lie表biao数shu据ju服fu务wu(注zhu意yi代dai码ma中zhong的de[]是shi全quan角jiao,使shi用yong的de时shi候hou替ti换huan成cheng半ban角jiao的de)
/// </summary>
[WebService(Namespace = "http:tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService] //<-这zhe段duan必bi须xu得de存cun在zai
public class ClientTypeCascadingDropDown : System.Web.Services.WebService
{
[WebMethod]
//一yi级ji客ke户hu类lei别bie相xiang关guan的deWebService方fang法fa
public CascadingDropDownNameValue[] ClientTypeRootList(string knownCategoryValues,string category) //<-除chu了leClientTypeRootList这zhe个ge方fang法fa名ming可ke变bian动dong,其qi它ta不bu能neng变bian动dong
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-这zhe段duan也ye得de有you,呵he呵he
//这zhe里li就jiu可ke以yi放fang你ni的de数shu据ju库ku相xiang关guan代dai码ma,比bi如ru把ba一yi级ji客ke户hu类lei别bie从cong数shu据ju库ku取qu出chu来lai然ran后hou存cun放fang在zai一yi个ge数shu组zu中zhong
//因yin为wei这zhe里li是shi一yi级ji客ke户hu的de下xia拉la列lie表biao,所suo以yi不bu用yong去qu管guan那na个gecategory的de值zhi
//Model.ClientType是shi我wo建jian的de一yi个ge实shi体ti类lei,其qi中zhong有youClientTypeName,ClientTypeID,ParentClientTypeID几ji个ge属shu性xing
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//当dang然ran你ni也ye可ke以yi使shi用yongDataSet、DataTabel等deng,在zaiforeach那na边bian把ba列lie表biao需xu要yao的de值zhi填tian充chong进jin去qu就jiu好hao
//下xia以yi部bu分fen是shi下xia拉la列lie表biao填tian充chong代dai码ma
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]
//二er级ji客ke户hu类lei别bie相xiang关guan的deWebService方fang法fa
public CascadingDropDownNameValue[] ClientTypeSubList(string knownCategoryValues, string category) //<-除chu了leClientTypeRootList这zhe个ge方fang法fa名ming可ke变bian动dong,其qi它ta不bu能neng变bian动dong
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-这zhe段duan也ye得de有you,呵he呵he
//二er级ji客ke户hu的de下xia拉la列lie表biao,得de取qu得de一yi级ji的decategory的de值zhi,以yi下xia代dai码ma是shi判pan断duan上shang级ji列lie表biao的decategory值zhi,存cun在zai或huo不bu是shi空kong的de话hua把ba值zhi赋fu给geiparentClientTypeID
//RootClientType是shi一yi级jiCascadingDropDown设she置zhi的decategory属shu性xing名ming称cheng
int parentClientTypeID;
if (!kv.ContainsKey("RootClientType") || !Int32.TryParse(kv["RootClientType"], out parentClientTypeID))
{
return null;
}
//这zhe里li就jiu可ke以yi放fang你ni的de数shu据ju库ku相xiang关guan代dai码ma,比bi如ru把ba一yi级ji客ke户hu类lei别bie从cong数shu据ju库ku取qu出chu来lai然ran后hou存cun放fang在zai一yi个ge数shu组zu中zhong
//Model.ClientType是shi我wo建jian的de一yi个ge实shi体ti类lei,其qi中zhong有youClientTypeName,ClientTypeID,ParentClientTypeID几ji个ge属shu性xing
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//当dang然ran你ni也ye可ke以yi使shi用yongDataSet、DataTabel等deng,在zaiforeach那na边bian把ba列lie表biao需xu要yao的de值zhi填tian充chong进jin去qu就jiu好hao
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
foreach (Model.ClientType model in models)
{
values.Add(new CascadingDropDownNameValue(model.ClientTypeName, model.ClientTypeID.ToString()));
}
return values.ToArray();
}
}基ji本ben上shang一yi个geCascadingDropDown控kong件jian就jiu会hui应ying对dui一yi个geWebserivce的de方fang法fa,如ru果guo再zai有you第di三san个ge,第di四si个geCascadingDropDown,按anClientTypeSubList为wei第di三san个ge,第di四si个geCascadingDropDown添tian加jia对dui应yingWebService方fang法fa
出chu处chu:http://blog.breakn.net/article.asp?id=389
【原文】