`
zjnbshifox
  • 浏览: 316109 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

AJAX Toolkit(2)--CascadeDropDown

    博客分类:
  • .NET
阅读更多
使用的数据库是SQL Server 2000的自带示例数据库Northwind,首先在一个下拉列表里面显示左右顾客(Customer),选择了相应的Customer后,显示该Customer的所有合同
首先,ASP.NET页面的组织:
xml 代码
  1. <form id="form1" runat="server">
  2. <div>
  3. <asp:ScriptManager ID="ScriptManager1" runat="server">
  4. <!----></asp:ScriptManager>
  5. <!---->div>
  6. <asp:DropDownList ID="DropDownList1" runat="server">
  7. <!----></asp:DropDownList> <!---->
  8. <asp:DropDownList ID="DropDownList2" runat="server">
  9. <!----></asp:DropDownList><!---->
  10. <ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
  11. Category="Customer" PromptText="Please select a Customers" LoadingText="[Loading Customers...]"
  12. ServicePath="WebService.asmx" ServiceMethod="GetCustomers" />
  13. <!---->
  14. <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
  15. Category="Order" PromptText="Please select a order" LoadingText="[Loading orders...]"
  16. ServiceMethod="GetOrders" ParentControlID="DropDownList1" ServicePath="WebService.asmx" />
  17. <!---->
  18. </form>
这里需要注意的是,需要设置第二个的CascadeDropDown的ParentControlID属性为父类的DropdownList ,如ParentControlID="DropDownList1"
下面建立一个WebService:
asmx文件
xml 代码
  1. <%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
WebService.cs文件
c# 代码
  1. using System;
  2. using System.Web;
  3. using System.Collections.Generic;
  4. using System.Web.Services;
  5. using System.Web.Services.Protocols;
  6. using System.Data.SqlClient;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Collections.Specialized;
  10. /// <summary></summary>
  11. /// WebService 的摘要说明
  12. ///
  13. [WebService(Namespace = "http://tempuri.org/")]
  14. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  15. [System.Web.Script.Services.ScriptService]
  16. public class WebService : System.Web.Services.WebService {
  17. public WebService () {
  18. //如果使用设计的组件,请取消注释以下行
  19. //InitializeComponent();
  20. }
  21. [WebMethod]
  22. [System.Web.Script.Services.ScriptMethod]
  23. public AjaxControlToolkit.CascadingDropDownNameValue[] GetCustomers(string knownCategoryValues, string category)
  24. {
  25. SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["my"].ConnectionString);
  26. SqlCommand cm = cn.CreateCommand(); //Customer是顶级目录,所以不需要有参数
  27. cm.CommandText = "select customerID,CompanyName from customers";
  28. cn.Open();
  29. SqlDataReader sdr = cm.ExecuteReader();
  30. List<ajaxcontroltoolkit.cascadingdropdownnamevalue> list = new List<ajaxcontroltoolkit.cascadingdropdownnamevalue>(); </ajaxcontroltoolkit.cascadingdropdownnamevalue></ajaxcontroltoolkit.cascadingdropdownnamevalue>
  31. while (sdr.Read())
  32. {
  33. list.Add(new AjaxControlToolkit.CascadingDropDownNameValue(sdr.GetString(1),sdr.GetString(0)));
  34. }
  35. return list.ToArray();
  36. }
  37. [WebMethod]
  38. [System.Web.Script.Services.ScriptMethod]
  39. public AjaxControlToolkit.CascadingDropDownNameValue[] GetOrders(string knownCategoryValues, string category)
  40. {
  41. //解析上一级的选择,注意是上一级Customer下拉列表中的Category属性
  42. StringDictionary kv = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
  43. string customer = "";
  44. if (!kv.ContainsKey("Customer"))
  45. {
  46. return null;
  47. }
  48. customer = kv["Customer"];
  49. SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["my"].ConnectionString);
  50. SqlCommand cm = cn.CreateCommand();
  51. cm.CommandText = "select OrderID,OrderDate from orders where CustomerID='" + customer + "'";
  52. cn.Open();
  53. SqlDataReader sdr = cm.ExecuteReader();
  54. List<ajaxcontroltoolkit.cascadingdropdownnamevalue> list = new List<ajaxcontroltoolkit.cascadingdropdownnamevalue>(); </ajaxcontroltoolkit.cascadingdropdownnamevalue></ajaxcontroltoolkit.cascadingdropdownnamevalue>
  55. while (sdr.Read())
  56. {
  57. list.Add(new AjaxControlToolkit.CascadingDropDownNameValue(sdr.GetInt32(0).ToString() + sdr.GetDateTime(1).ToString(), sdr.GetInt32(0).ToString()));
  58. }
  59. return list.ToArray();
  60. }
  61. }
运行结果:
  • 大小: 26.3 KB
分享到:
评论
1 楼 hqsss123 2008-03-10  
不错,学习

相关推荐

Global site tag (gtag.js) - Google Analytics