日期:2014-05-17  浏览次数:20476 次

关于Ajax拓展控件CascadingDropDown级联下拉控件的问题
HTML code
    
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true" EnableScriptLocalization="true">
    </asp:ScriptManager>
       <table style="width: 100%;">
        <tr>
            <td>新闻类别:</td>
            <td>
                <asp:DropDownList ID="DropDownList1" runat="server">
                </asp:DropDownList>
                <ajax:CascadingDropDown ID="DropDownList1_CascadingDropDown" runat="server" 
                    Enabled="True" TargetControlID="DropDownList1" Category="category" PromptText="请选择一个新闻类别" LoadingText="载入中..." ScriptPath="MyCascadingWebService.asmx" ServiceMethod="GetNewCate">
                </ajax:CascadingDropDown>
            </td>
        </tr>
        <tr>
            <td>新闻标题:</td>
            <td>
                <asp:DropDownList ID="DropDownList2" runat="server">
                </asp:DropDownList>
               <ajax:CascadingDropDown ID="DropDownList2_CascadingDropDown" runat="server" 
                    Enabled="True" TargetControlID="DropDownList2" Category="title" PromptText="请选择一个新闻标题" LoadingText="载入中..." ScriptPath="MyCascadingWebService.asmx" ServiceMethod="GetNewTitle" ParentControlID="DropDownList1">
                </ajax:CascadingDropDown>
            </td>
        </tr>
    </table>


WebService
C# code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using AjaxControlToolkit;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Collections.Specialized;

namespace Ajax扩展控件
{
    /// <summary>
    /// MyCascadingWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class MyCascadingWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public CascadingDropDownNameValue[] GetNewCate(string knownCategoryValues, string category)
        {
            List<CascadingDropDownNameValue> list = new List<CascadingDropDownNameValue>();
            SqlConnection myCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=newsystem;Integrated Security=True");
            myCon.Open();
            SqlCommand myCmd = new SqlCommand("select name,id from catepory", myCon);
            SqlDataReader myDR = myCmd.ExecuteReader();
            while (myDR.Read())
            {
                list.Add(new CascadingDropDownNameValue(myDR["name"].ToString(), myDR["id"].ToString()));
            }
            myCon.Close();
            myDR.Close();
            return list.ToArray();
        }
        [WebMethod]
        public CascadingDropDownNameValue[] GetNewTitle(string knownCategoryValues, string category)
        {
            List<CascadingDropDownNameValue> list = new List<CascadingDropDownNameValue>();
            StringDictionary sdc = AjaxControlToolkit.CascadingDropDown.ParseKnownCate