ASP.NET / C# - Ajax级联DropDown - 第二个DDL没有填充

时间:2016-09-12 21:19:33

标签: c# asp.net ajax

我正在创建一个由级联DDL驱动的简单Web表单。第一个列表从给定值列表中填充;第二个是由SQL查询填充的,第一个DDL中的选定值作为第二个查询的参数。看起来很简单,拼凑在线的例子,但不幸的是,第二个DDL根本没有填充。我已检查过查询本身是否正常,并在WHERE子句/参数被删除时填充。

以下是代码:

C#

using AjaxControlToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Web_Form___Chart_Review
{
/// <summary>
/// Summary description for ProgramCS
/// </summary>
[WebService(Namespace = "http://tempuri.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class ProgramCS : System.Web.Services.WebService
{
    List<Division> division = new List<Division>();
    [WebMethod]
    public CascadingDropDownNameValue[] GetDivision(string knownCategoryValues, string category)
    {
        division.Add(new Division { DivID = "MH", Name = "Mental Health" });
        division.Add(new Division { DivID = "SUD", Name = "Substance Use Disorder" });
        division.Add(new Division { DivID = "GAM", Name = "Gambling" });
        division.Add(new Division { DivID = "%", Name = "LMP" });
        List<CascadingDropDownNameValue> l = new List<CascadingDropDownNameValue>();
        var x = from c in division select c;
        foreach (Division divisions in x)
        {
            l.Add(new CascadingDropDownNameValue(divisions.Name,divisions.DivID));

        }
        return l.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetSite(string knownCategoryValues, string category)
    {
        string sel_division = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["DivID"];
        string query = string.Format("SELECT DISTINCT [SiteName],[Site] FROM ChartReview.dbo.vw_ProgramList WHERE [Category] like '{0}'",sel_division);
        List<CascadingDropDownNameValue> sites = GetData(query);
        return sites.ToArray();
    }

    public class Division
    {
        public string DivID {get;set;}
        public string Name {get;set;}
    }

    private List<CascadingDropDownNameValue> GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        using (SqlConnection con = new SqlConnection(conString))
        {
            con.Open();
            cmd.Connection = con;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    values.Add(new CascadingDropDownNameValue
                    {
                        name = reader[0].ToString(),
                        value = reader[1].ToString()
                    });
                }
                reader.Close();
                con.Close();
                return values;
            }
        }
    }

}
}

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Web_Form___Chart_Review.Default" EnableEventValidation="false" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="asm" runat="server"></asp:ScriptManager>
<div>
        For which division are you creating a tally?<br />
    <asp:DropDownList ID="ddlSelectDivision" runat="server"></asp:DropDownList>
    <ajaxToolkit:CascadingDropDown ID="ddlSelectDivision_CascadingDropDown" runat="server" TargetControlID="ddlSelectDivision" ServicePath="ProgramCS.asmx" ServiceMethod="GetDivision" Category="Division" PromptText="Select Division" SelectedValue="MH" PromptValue="MH" LoadingText="Loading..." />
    <br />
    <asp:DropDownList ID="ddlSelectSite" runat="server"></asp:DropDownList>
    <ajaxToolkit:CascadingDropDown ID="ddlSelectSite_CascadingDropDown" runat="server" TargetControlID="ddlSelectSite" ServicePath="ProgramCS.asmx" ServiceMethod="GetSite" Category="Site" PromptText="Select Site" PromptValue="" LoadingText="Loading..." />



    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这可能就像确保您的第一个下拉列表具有AutoPostBack =&#34; true&#34;一样简单。确保第二个选择值。