(1)只能通过Web Service的方式来使用这个控件
(2)创建的Web Service 如下
[System.Web.Script.Services.ScriptService]
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class BasicTypes : System.Web.Services.WebService
{
public BasicTypes()
{
// Uncomment the following line if using designed components
// InitializeComponent();
}
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents( string knownCategoryValues, string category)
{
DataTable dt = GetDataTable( " Select * from BasicType " );
List < AjaxControlToolkit.CascadingDropDownNameValue > values = new List < AjaxControlToolkit.CascadingDropDownNameValue > ();
foreach (DataRow dr in dt.Rows)
{
values.Add( new AjaxControlToolkit.CascadingDropDownNameValue(dr[ " Type " ].ToString(), dr[ " Id " ].ToString()));
}
return values.ToArray();
}
}
注意红色代码部分。
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class BasicTypes : System.Web.Services.WebService
{
public BasicTypes()
{
// Uncomment the following line if using designed components
// InitializeComponent();
}
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents( string knownCategoryValues, string category)
{
DataTable dt = GetDataTable( " Select * from BasicType " );
List < AjaxControlToolkit.CascadingDropDownNameValue > values = new List < AjaxControlToolkit.CascadingDropDownNameValue > ();
foreach (DataRow dr in dt.Rows)
{
values.Add( new AjaxControlToolkit.CascadingDropDownNameValue(dr[ " Type " ].ToString(), dr[ " Id " ].ToString()));
}
return values.ToArray();
}
}
在CascadingDropdownList的Page method方法中,也就是在使用这个控件的页面的后台代码中,调用Web Service中的(3)WebMethod的名字必须与控件中设置的WebMethod相同。如下图中粗体紫色部分所示:
<
ajaxToolkit:CascadingDropDown
ID
="ccdDataTypeParents"
runat
="server"
Enabled
="True"
ServiceMethod ="GetDropdownParents" ParentControlID =""
UseContextKey ="True" ServicePath ="BasicTypes.asmx"
TargetControlID ="ddlDataTypeParents"
Category ="DataTypeParentsId" PromptText ="请选择父类型"
LoadingText ="正在载入父类型" SelectedValue ="" >
</ ajaxToolkit:CascadingDropDown >
(4)在PageMethod中的使用方法如下:
ServiceMethod ="GetDropdownParents" ParentControlID =""
UseContextKey ="True" ServicePath ="BasicTypes.asmx"
TargetControlID ="ddlDataTypeParents"
Category ="DataTypeParentsId" PromptText ="请选择父类型"
LoadingText ="正在载入父类型" SelectedValue ="" >
</ ajaxToolkit:CascadingDropDown >
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static AjaxControlToolkit.CascadingDropDownNameValue[] GetDropdownParents( string knownCategoryValues, string category)
{
return new BasicTypes().GetDropDownParents(knownCategoryValues, category);
}
注意要加入紫色代码部分。系统会自己生成紫色的部分,但是不同,所以把系统自动生成 的替换掉就行了(没有替换的我没有试过,只是目前这样做可以工作)。
[System.Web.Script.Services.ScriptMethod]
public static AjaxControlToolkit.CascadingDropDownNameValue[] GetDropdownParents( string knownCategoryValues, string category)
{
return new BasicTypes().GetDropDownParents(knownCategoryValues, category);
}
当出现500错误的时候,核对一下上面所讲的注意事项。