| 订阅 | 在线投稿
分享
 
 
 

asp.net AJAX中的CascadingDropDown控件使用心得

来源:互联网  宽屏版  评论
2008-12-13 08:23:46

本文为【asp.net AJAX中的CascadingDropDown控件使用心得】的汉字拼音对照版显示拼音

jibenzenmeyongzhelijiubuluosuolewangshangyouhenduowenzhangjieshaobaokuoasp.netnabianyeyoushilikeyixiazaizhelichongdianshuoshuoCategoryzhegeshuxingjiruhegoujianwebserviceCascadingDropDowndehewebservicepeihe使shiyongcaixingxiankanyemiankongjiandaimaview plaincopy to clipboardprint?

<!--xialaliebiaokongjian-->

<asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList> 

<asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList> 

<!--duiyingdeCascadingDropDownkongjian-->

<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" LoadingText="jiazaizhong" PromptText="qingxuanze"

ServiceMethod="ClientTypeRootList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"

TargetControlID="ddlRootClass" Category="RootClientType">

</cc1:CascadingDropDown>

<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" LoadingText="jiazaizhong" PromptText="qingxuanze"

ServiceMethod="ClientTypeSubList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"

TargetControlID="ddlSubClass" Category="SubClientType" ParentControlID="ddlRootClass">

</cc1:CascadingDropDown>

<!--xialaliebiaokongjian-->

<asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList> 

<asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList> 

<!--duiyingdeCascadingDropDownkongjian-->

<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" LoadingText="jiazaizhong" PromptText="qingxuanze"

ServiceMethod="ClientTypeRootList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"

TargetControlID="ddlRootClass" Category="RootClientType">

</cc1:CascadingDropDown>

<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" LoadingText="jiazaizhong" PromptText="qingxuanze"

ServiceMethod="ClientTypeSubList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"

TargetControlID="ddlSubClass" Category="SubClientType" ParentControlID="ddlRootClass">

</cc1:CascadingDropDown>zhuyiCascadingDropDownrkongjianzhongdeCategoryshezhiCategoryzhuyaojiushiweiniCascadingDropDownrkongjianduiyingdexialaliebiaokongjianxuandingdezhiqugemingzihaoqufenshixialaliebiaodezhisuoyizhegedequdebuyiyangServiceMethodzhuyaojiushiduiyingWebSerivcedefangfalezhimingdangqianCascadingDropDownkongjian使shiyongnageWebSerivcezhongdefangfaqitademejiubuxishuole

zailaikanWebServicededaimaview plaincopy to clipboardprint?

/// <summary>

/// ClientType Ajaxxialaliebiaoshujufuwuzhuyidaimazhongdeshiquanjiao使shiyongdeshihoutihuanchengbanjiaode

/// </summary>

WebService(Namespace = "http:tempuri.org/")

WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)

System.Web.Script.Services.ScriptService //<-zheduanbixudecunzai

public class ClientTypeCascadingDropDown : System.Web.Services.WebService

{

WebMethod

//yijikehuleibiexiangguandeWebServicefangfa

public CascadingDropDownNameValue ClientTypeRootList(string knownCategoryValues,string category) //<-chuleClientTypeRootListzhegefangfamingkebiandongqitabunengbiandong

{

StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-zheduanyedeyouhehe

//zhelijiukeyifangnideshujukuxiangguandaimabirubayijikehuleibiecongshujukuquchulairanhoucunfangzaiyigeshuzuzhong

//yinweizhelishiyijikehudexialaliebiaosuoyibuyongquguannagecategorydezhi

//Model.ClientTypeshiwojiandeyigeshitileiqizhongyouClientTypeName,ClientTypeID,ParentClientTypeIDjigeshuxing

//Model.ClientType model = new Model.ClientType();

//Model.ClientType models = new Model.ClientType;

//dangranniyekeyi使shiyongDataSetDataTabeldengzaiforeachnabianbaliebiaoxuyaodezhitianchongjinqujiuhao

//xiayibufenshixialaliebiaotianchongdaima

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

//erjikehuleibiexiangguandeWebServicefangfa

public CascadingDropDownNameValue ClientTypeSubList(string knownCategoryValues, string category) //<-chuleClientTypeRootListzhegefangfamingkebiandongqitabunengbiandong

{

StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-zheduanyedeyouhehe

//erjikehudexialaliebiaodequdeyijidecategorydezhiyixiadaimashipanduanshangjiliebiaodecategoryzhicunzaihuobushikongdehuabazhifugeiparentClientTypeID

//RootClientTypeshiyijiCascadingDropDownshezhidecategoryshuxingmingcheng

int parentClientTypeID;

if (!kv.ContainsKey("RootClientType") || !Int32.TryParse(kv"RootClientType", out parentClientTypeID))

{

return null;

}

//zhelijiukeyifangnideshujukuxiangguandaimabirubayijikehuleibiecongshujukuquchulairanhoucunfangzaiyigeshuzuzhong

//Model.ClientTypeshiwojiandeyigeshitileiqizhongyouClientTypeName,ClientTypeID,ParentClientTypeIDjigeshuxing

//Model.ClientType model = new Model.ClientType();

//Model.ClientType models = new Model.ClientType;

//dangranniyekeyi使shiyongDataSetDataTabeldengzaiforeachnabianbaliebiaoxuyaodezhitianchongjinqujiuhao

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 Ajaxxialaliebiaoshujufuwuzhuyidaimazhongdeshiquanjiao使shiyongdeshihoutihuanchengbanjiaode

/// </summary>

WebService(Namespace = "http:tempuri.org/")

WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)

System.Web.Script.Services.ScriptService //<-zheduanbixudecunzai

public class ClientTypeCascadingDropDown : System.Web.Services.WebService

{

WebMethod

//yijikehuleibiexiangguandeWebServicefangfa

public CascadingDropDownNameValue ClientTypeRootList(string knownCategoryValues,string category) //<-chuleClientTypeRootListzhegefangfamingkebiandongqitabunengbiandong

{

StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-zheduanyedeyouhehe

//zhelijiukeyifangnideshujukuxiangguandaimabirubayijikehuleibiecongshujukuquchulairanhoucunfangzaiyigeshuzuzhong

//yinweizhelishiyijikehudexialaliebiaosuoyibuyongquguannagecategorydezhi

//Model.ClientTypeshiwojiandeyigeshitileiqizhongyouClientTypeName,ClientTypeID,ParentClientTypeIDjigeshuxing

//Model.ClientType model = new Model.ClientType();

//Model.ClientType models = new Model.ClientType;

//dangranniyekeyi使shiyongDataSetDataTabeldengzaiforeachnabianbaliebiaoxuyaodezhitianchongjinqujiuhao

//xiayibufenshixialaliebiaotianchongdaima

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

//erjikehuleibiexiangguandeWebServicefangfa

public CascadingDropDownNameValue ClientTypeSubList(string knownCategoryValues, string category) //<-chuleClientTypeRootListzhegefangfamingkebiandongqitabunengbiandong

{

StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-zheduanyedeyouhehe

//erjikehudexialaliebiaodequdeyijidecategorydezhiyixiadaimashipanduanshangjiliebiaodecategoryzhicunzaihuobushikongdehuabazhifugeiparentClientTypeID

//RootClientTypeshiyijiCascadingDropDownshezhidecategoryshuxingmingcheng

int parentClientTypeID;

if (!kv.ContainsKey("RootClientType") || !Int32.TryParse(kv"RootClientType", out parentClientTypeID))

{

return null;

}

//zhelijiukeyifangnideshujukuxiangguandaimabirubayijikehuleibiecongshujukuquchulairanhoucunfangzaiyigeshuzuzhong

//Model.ClientTypeshiwojiandeyigeshitileiqizhongyouClientTypeName,ClientTypeID,ParentClientTypeIDjigeshuxing

//Model.ClientType model = new Model.ClientType();

//Model.ClientType models = new Model.ClientType;

//dangranniyekeyi使shiyongDataSetDataTabeldengzaiforeachnabianbaliebiaoxuyaodezhitianchongjinqujiuhao

List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

foreach (Model.ClientType model in models)

{

values.Add(new CascadingDropDownNameValue(model.ClientTypeName, model.ClientTypeID.ToString()));

}

return values.ToArray();

}

}jibenshangyigeCascadingDropDownkongjianjiuhuiyingduiyigeWebserivcedefangfaruguozaiyoudisangedisigeCascadingDropDownanClientTypeSubListweidisangedisigeCascadingDropDowntianjiaduiyingWebServicefangfa

chuchuhttp://blog.breakn.net/article.asp?id=389

原文
 
  基本怎么用这里就不啰嗦了,网上有很多文章介绍,包括asp.net那边也有示例可以下载,这里重点说说Category这个属性及如何构建webservice,CascadingDropDown得和webservice配合使用才行。先看页面控件代码view plaincopy to clipboardprint? <!--下拉列表控件--> <asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList>&nbsp; <asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList>&nbsp; <!--对应的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>&nbsp; <asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList>&nbsp; <!--对应的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方法 出处:[url=http://blog.breakn.net/article.asp?id=389]http://blog.breakn.net/article.asp?id=389[/url]
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有