CascadingDropDown


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

官方: CascadingDropDown DemonstrationUsing CascadingDropDown with a Database

http://www.ajaxasp.net.cn/Demos/CascadingDropDown/CascadingDropDown.aspx

斜体 属性为可选属性:

<ajaxToolkit:CascadingDropDown ID="CDD1" runat="server"

TargetControlID="DropDownList2"

Category="Model"

PromptText="Please select a model"

LoadingText="[Loading models...]"

ServicePath="CarsService.asmx"

ServiceMethod="GetDropDownContents"

ParentControlID="DropDownList1"

SelectedValue="SomeValue" />
  • TargetControlID - 扩展的 DropDownList 控件 ID
  • Category - 对应的数据的分类,例如上面例子中的“区域”、“省份”或者“城市”
  • PromptText - 在 DropDownList 被选择前提示用户的信息
  • PromptValue - 对应提示信息的 Value,这里指的是 DropDownList 中一个 ListItem 中的 Value 值
  • EmptyText - 当从 Web Service 中获得数据为空的时候 DropDownList 显示的信息,可选
  • EmptyValue - 对应当数据项为空的时候 DropDownList 中显示 EmptyText 的信息的 Value,这里指的是 DropDownList 中一个 ListItem 中的 Value 值,可选
  • LoadingText - 当 CascadingDropDown 在和 Web Service 进行数据通信的时候显示的信息
  • ServicePath - 对应的获得数据的 Web Service 路径,如果对应的 Web Service 方法为当前页的 Web Service 方法时,需要将其指定为空
  • ServiceMethod - 对应的获得 Web Service 方法名,它需要指定为如下的函数签名
    [System.Web.Services.WebMethod]

    [System.Web.Script.Services.ScriptMethod]

    public CascadingDropDownNameValue[] GetDropDownContents(

    string knownCategoryValues, string category) { ... }
    注意:
    你可以替换上面示例中的方法名称,GetDropDownContents 为任何你想要的方法名,但是返回值和参数列表需要保持完全一致。

  • ContextKey - 一个可用于传入 Web Service 方法的参数,该参数将用于 Web Service 根据客户端的不同操作产生相应的不同的返回结果,当采用 ContextKey 的时候,这个方法的函数签名如下表示:
    [System.Web.Services.WebMethod]

    [System.Web.Script.Services.ScriptMethod]

    public CascadingDropDownNameValue[] GetDropDownContents(

    string knownCategoryValues,

    string category, string contextKey) { ... }
    注意:
    你可以替换上面示例中的方法名称,GetDropDownContents 为任何你想要的方法名,但是返回值和参数列表需要保持完全一致。

  • UseContextKey - 指定是否采用 ContextKey,当函数签名采用了带有 ContextKey 的函数签名时,这个属性将被自动设置为 True
  • ParentControlID - 上一级 DropDownList 控件的 ID,当其没有上一级 DropDownList 的时候,该属性被设置为空,例如上面例子中的 “区域”
  • SelectedValue - 默认的初始化选中值的 Value,这里指的是 DropDownList 中 ListItem 的 Value 值

自己写的代码:

服务端代码:


        [WebMethod]
        [System.Web.Script.Services.ScriptMethod()]
        
public static AjaxControlToolkit.CascadingDropDownNameValue[] GetParentService(string knownCategoryValues, string category)
        
{
            System.Data.DataSet ds 
= Agr.AgrOperate.GetServiceItemsBy(0);
            
if (ds.Tables[0].Rows.Count > 0)
            
{
                System.Collections.Generic.List
<AjaxControlToolkit.CascadingDropDownNameValue> Values
                    
= new System.Collections.Generic.List<AjaxControlToolkit.CascadingDropDownNameValue>();
                Values.Add(
new AjaxControlToolkit.CascadingDropDownNameValue("""-1"));
                
foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
                
{
                    Values.Add(
new AjaxControlToolkit.CascadingDropDownNameValue(dr["ItemsName"].ToString(), dr["PID"].ToString()));
                }

                Values.Add(
new AjaxControlToolkit.CascadingDropDownNameValue("其它""99"));

                
return Values.ToArray();
            }

            
else
            
{
                
return null;
            }

        }


        
//加载子受理项目
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod()]
        
public static AjaxControlToolkit.CascadingDropDownNameValue[] GetSubService(string knownCategoryValues, string category)
        
{
            System.Collections.Specialized.StringDictionary kv
                
= AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            
int PID;
            
//"ParentID"是上一级CascadingDropDown设置的category属性名称 
            if (!kv.ContainsKey("ParentID"|| !Int32.TryParse(kv["ParentID"], out PID))
            
{
                
return null;
            }


            System.Data.DataSet ds 
= Agr.AgrOperate.GetServiceItemsBy(PID);
            
if (ds.Tables[0].Rows.Count > 0)
            
{
                System.Collections.Generic.List
<AjaxControlToolkit.CascadingDropDownNameValue> Values
                    
= new System.Collections.Generic.List<AjaxControlToolkit.CascadingDropDownNameValue>();
                Values.Add(
new AjaxControlToolkit.CascadingDropDownNameValue("""-1"));
                
foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
                
{
                    Values.Add(
new AjaxControlToolkit.CascadingDropDownNameValue(dr["ItemsName"].ToString(), dr["PID"].ToString()));
                }


                
return Values.ToArray();
            }

            
else
            
{
                
return null;
            }

        }

 HTML页面代码:


                
<td align="right">
                    协议受理项目
                
</td>
                
<td align="left">
                    
<asp:DropDownList ID="drplstParentService" runat="server" BackColor="#FFFFC0" CssClass="cssInputBox"
                        ToolTip
="请选择要查询的受理项目" Width="100%">
                    
</asp:DropDownList>
                    
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="drplstParentService"
                     Category
="ParentID" LoadingText="加载中" PromptText="" ServiceMethod="GetParentService">
                    
</cc1:CascadingDropDown>
                
</td>
                
<td align="right">
                    子受理项目
                
</td>
                
<td align="left">
                    
<asp:DropDownList ID="drplstSubService" runat="server" BackColor="#FFFFC0" CssClass="cssInputBox"
                        ToolTip
="请选择要查询的子受理项目" Width="100%">
                    
</asp:DropDownList>
                    
<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="drplstSubService" 
                    Category
="SubID" LoadingText="加载中" PromptText="" ServiceMethod="GetSubService" ParentControlID="drplstParentService">
                    
</cc1:CascadingDropDown>
                
</td>

 另:使用 CascadingDropDown 要设置 EnableEventValidation="false" ,哪位高人知道有其他做法吗?

原文地址:https://www.cnblogs.com/nikytwo/p/1468846.html