·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> asp.net AJAX中的CascadingDropDown控件使用心得

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

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

  基本怎么用这里就不啰嗦了,网上有很多文章介绍,包括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方法\

出处:http://blog.breakn.net/article.asp?id=389