Asp.net 的ajax 有ajax.net(ajaxPro),atlas等版本。Asp.net ajax 1.0 是Atalas的后续,功能强大,上手容易,建议学之。CascadingDropDown是其中的一个功能,为层叠下拉列表框。但是Samle是用XML作为数据源的,不符合我的要求(唉,什么时候我能掌握好XML啊),所以写了这个。希望能给大家带来帮助
数据库SQLSERVER 2000,其中有表T_company,T_department,T_user_customer,三表依次以前表ID连接关系。为了实现一个公司==》部门==》员工的三级下拉框,准备了如下步骤。
1、准备数据对象。
采用Castle.activerecod工具。这样可以省去写SQL语句的烦恼。
Company对象:
using System.Collections.Generic;
using System.Text;
using Castle.ActiveRecord;
using System.Collections;
namespace util.DataVO
... {
/**//// <summary>
/// Show the list of company
/// </summary>
[ActiveRecord("T_company")]
public class T_company : ActiveRecordBase<T_company>
...{
private string companyName;
private int companyId;
/**//// <summary>
/// 公司名称
/// </summary>
[Property]
public string CompanyName
...{
get ...{ return companyName; }
set ...{ companyName = value; }
}
/**//// <summary>
/// 公司ID
/// </summary>
[PrimaryKey(PrimaryKeyType.Assigned)]
public int CompanyId
...{
get ...{ return companyId; }
set ...{ companyId = value; }
}
}
department对象:
using System.Collections.Generic;
using System.Text;
using Castle.ActiveRecord;
namespace util.DataVO
... {
/**//// <summary>
/// show department
/// </summary>
[ActiveRecord("T_department")]
public class T_department : ActiveRecordBase<T_department>
...{
private string departmentName;
private int departmentId;
private int companyId;
/**//// <summary>
/// 公司ID
/// </summary>
[Property]
public int CompanyId
...{
get ...{ return companyId; }
set ...{ companyId = value; }
}
/**//// <summary>
/// 部门名称
/// </summary>
[Property]
public string DepartmentName
...{
get ...{ return departmentName; }
set ...{ departmentName = value; }
}
/**//// <summary>
/// 部门ID
/// </summary>
[PrimaryKey(PrimaryKeyType.Assigned)]
public int DepartmentId
...{
get ...{ return departmentId; }
set ...{ departmentId = value; }
}
}
}
T_user_Customer对象
using System.Collections.Generic;
using System.Text;
using Castle.ActiveRecord;
using System.Collections;
namespace util.DataVO
... {
/**//// <summary>
/// user Account
/// </summary>
[ActiveRecord("T_User_Customer")]
public class T_User_Customer : ActiveRecordBase<T_User_Customer>
...{
private string nameCn;
private int customerId;
private int companyId;
private int departmentId;
/**//// <summary>
/// 员工名
/// </summary>
[Property]
public string NameCn
...{
set ...{ nameCn = value; }
get ...{ return nameCn; }
}
/**//// <summary>
/// 员工ID
/// </summary>
[PrimaryKey(PrimaryKeyType.Assigned)]
public int CustomerId
...{
set ...{ customerId = value; }
get ...{ return customerId; }
}
/**//// <summary>
/// 公司ID
/// </summary>
[Property]
public int CompanyId
...{
get ...{ return companyId; }
set ...{ companyId = value; }
}
/**//// <summary>
/// 部门ID
/// </summary>
[Property]
public int DepartmentId
...{
get ...{ return departmentId; }
set ...{ departmentId = value; }
}
/**//// <summary>
/// 取得员工名称列表
/// </summary>
/// <returns></returns>
public static string[] findAllNamelist()
...{
T_User_Customer[] list = FindAll();
string[] nameList = new string[list.Length];
for (int i = 0; i < list.Length; i++)
...{
nameList[i] = list[i].NameCn;
}
return nameList;
}
}
}
在Global.asax里面添加初始化,初始化一个命名空间就可以了
System.Configuration.ConfigurationManager.GetSection( " activerecord " ) as Castle.ActiveRecord.Framework.IConfigurationSource;
Castle.ActiveRecord.ActiveRecordStarter.Initialize( typeof (util.DataVO.T_company).Assembly, source);
2、在ASPX页面上加下拉列表框
<% @ Register Assembly = " AjaxControlToolkit " Namespace = " AjaxControlToolkit " TagPrefix = " ajaxToolkit " %>
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head id = " Head1 " runat = " server " >
< title > 无标题页 </ title >
< link href = " ../../common/css/okcss.css " rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< script type = " text/javascript " src = " ../../common/js/dmCalendarForAllDate.js " ></ script >
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
< link href = " /aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css "
rel = " stylesheet " type = " text/css " />
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:ScriptManager ID = " ScriptManager1 " runat = " server " EnablePartialRendering = " true " />
< table cellpadding = " 4 " cellspacing = " 1 " class = " table3 " style = " border-right: 0px; border-top: 0px;
border - left: 0px; width: 100 % ; border - bottom: 0px; " >
< tr >
< td align = " center " bgcolor = " #9cceff " >
< strong > 报表分析 </ strong ></ td >
</ tr >
< tr >
< td align = " center " >
< table style = " width: 100% " border = " 1 " >
< tr >
< td style = " width: 77px " >
公司: </ td >
< td style = " width: 92px " >
< asp:DropDownList ID = " DropDownListCompany " runat = " server " Width = " 132px " >
</ asp:DropDownList ></ td >
< ajaxToolkit:CascadingDropDown
ID = " CascadingDropDown1 "
runat = " server "
TargetControlID = " DropDownListCompany "
Category = " Company "
PromptText = " 请选择一个公司 "
ServicePath = " GetCascadeService.asmx "
ServiceMethod = " GetCompanyList " />
< td style = " width: 67px " >
& nbsp; 日 & nbsp; & nbsp;期 : </ td >
< td colspan = " 2 " >
从: < asp:TextBox ID = " TextBoxDateFrom " runat = " server " MaxLength = " 10 " onfocus = " calendar() " Width = " 69px " ></ asp:TextBox > 到: < asp:TextBox ID = " TextBoxDateTo " runat = " server " MaxLength = " 10 " onfocus = " calendar() " Width = " 69px " ></ asp:TextBox ></ td >
< td style = " width: 100px " >
</ td >
</ tr >
< tr >
< td style = " width: 77px; height: 17px " >
部门名称: </ td >
< td style = " width: 92px; height: 17px " >
< asp:DropDownList ID = " DropDownListDepartment " runat = " server " Width = " 132px " >
</ asp:DropDownList ></ td >
< ajaxToolkit:CascadingDropDown
ID = " CascadingDropDown2 "
runat = " server "
TargetControlID = " DropDownListDepartment "
ParentControlID = " DropDownListCompany "
PromptText = " 请选择部门 "
ServiceMethod = " GetDepartmentList "
ServicePath = " GetCascadeService.asmx "
Category = " Department " />
< td style = " width: 67px; height: 17px " >
员工名称: </ td >
< td style = " height: 17px " colspan = " 2 " >
< asp:DropDownList ID = " DropDownListCustomer " runat = " server " Width = " 132px " >
</ asp:DropDownList ></ td >
< ajaxToolkit:CascadingDropDown
ID = " CascadingDropDown3 "
runat = " server "
TargetControlID = " DropDownListCustomer "
ParentControlID = " DropDownListDepartment "
PromptText = " 请选择员工 "
ServiceMethod = " GetCustomerList "
ServicePath = " GetCascadeService.asmx "
Category = " Customer " />
< td style = " width: 100px; height: 17px " >
< asp:ImageButton ID = " ImageBSearch " runat = " server " ImageUrl = " ~/common/img/button/back/serch.gif "
OnClick = " ImageBSearch_Click " /></ td >
</ tr >
< tr >
< td style = " width: 77px " >
</ td >
< td style = " width: 92px " >
</ td >
< td style = " width: 67px " >
</ td >
< td style = " width: 100px " >
</ td >
< td style = " width: 100px " >
</ td >
< td style = " width: 100px " >
</ td >
</ tr >
</ table >
</ td >
</ tr >
< tr >
< td align = " center " >
</ td >
</ tr >
</ table >
</ div >
</ form >
</ body >
</ html >
3、编写对应的webservice
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using AjaxControlToolkit;
using util.DataVO;
using System.Collections.Generic;
using System.Web.Script.Services;
using System.Collections.Specialized;
/**/ /// <summary>
/// GetCascadeService 的摘要说明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class GetCascadeService : System.Web.Services.WebService
... {
public GetCascadeService()
...{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public CascadingDropDownNameValue[] GetCompanyList(string knownCategoryValues,
string category)
...{
List<CascadingDropDownNameValue> values =new List<CascadingDropDownNameValue>();
T_company[] list = T_company.FindAll();
foreach(T_company tc in list)
...{
string name=tc.CompanyName;
int id = tc.CompanyId;
values.Add(new CascadingDropDownNameValue(name, id.ToString()));
}
return values.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetDepartmentList(string knownCategoryValues,
string category)
...{
StringDictionary kv =CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int companyId;
if (!kv.ContainsKey("Company") || !Int32.TryParse(kv["Company"], out companyId))
...{
return null;
}
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
T_department[] list = T_department.FindAllByProperty("CompanyId",companyId);
foreach (T_department td in list)
...{
string name = td.DepartmentName;
int id = td.DepartmentId;
values.Add(new CascadingDropDownNameValue(name, id.ToString()));
}
return values.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetCustomerList(string knownCategoryValues,
string category)
...{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int departmentId;
if (!kv.ContainsKey("Department") || !Int32.TryParse(kv["Department"], out departmentId))
...{
return null;
}
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
T_User_Customer[] list = T_User_Customer.FindAllByProperty("DepartmentId", departmentId);
foreach (T_User_Customer td in list)
...{
string name = td.NameCn;
int id = td.CustomerId;
values.Add(new CascadingDropDownNameValue(name, id.ToString()));
}
return values.ToArray();
}
}
注意:
(1)Castle.activerecord需要初始化
(2)ASPX页面上要加
(3)Webservice的类名前面添加
[System.Web.Script.Services.ScriptService()],不然报下拉框中文字为“[Method Error 12031]”或“[Method Error 500]”
(4)目前关于Ajax的版本很多,AjaxPro,atalas,asp.net ajax,建议了解一下相互之前的关系,选一样学习。
本文采用的是http://asp.net/ajax/downloads/default.aspx上面下载的Asp.net ajax 其中Sample很多,也便于学习