简体   繁体   中英

Cascading DropDownList not populating

I've been working on this for several days with no luck. I'm simply trying to get a couple Cascading DropDownList to work -- that's it. However, the DropDownLists won't even display the "PromptText" text and it definitely won't populate.

I've copied and pasted several examples from the web and I get the same problem: empty dropdownlists. No error messages. What am I doing incorrect?

My server is running IIS: 7.0, ASP.Net Runtime Version: 4.0/4.5

The most recent tutorial I've followed is located here

Here's my code from my Webservice:

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

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]
public class CCDDLWebService : System.Web.Services.WebService {
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);

[WebMethod]
public CascadingDropDownNameValue[] BindCountry(string knownCategoryValues, string category) 
{
    DataSet ds = new DataSet();
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT * from Countries", con);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    cmd.ExecuteNonQuery();       
    adp.Fill(ds);
    con.Close();
    List<CascadingDropDownNameValue> CountryDetails = new List<CascadingDropDownNameValue>();
    foreach (DataRow DR in ds.Tables[0].Rows)
    {
        string CountryID = DR["ID"].ToString();
        string CountryName = DR["Name"].ToString();
        CountryDetails.Add(new CascadingDropDownNameValue(CountryName, CountryID));
    }
    return CountryDetails.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue[] BindState(string knownCategoryValues, string category)
{
    DataSet ds = new DataSet();
    int CountryID;
    StringDictionary CountryDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
    CountryID = Convert.ToInt32(CountryDetails["Country"]);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from States where Country=@CountryID", con);
    cmd.Parameters.AddWithValue("@CountryID", CountryID);
    cmd.ExecuteNonQuery();
    SqlDataAdapter adp = new SqlDataAdapter(cmd);       
    adp.Fill(ds);
    con.Close();

    List<CascadingDropDownNameValue> StateDetails = new List<CascadingDropDownNameValue>();

    foreach (DataRow DR in ds.Tables[0].Rows)
    {
        string stateID = DR["ID"].ToString();
        string statename = DR["State"].ToString();
        StateDetails.Add(new CascadingDropDownNameValue(statename, stateID));
    }
    return StateDetails.ToArray();
}
}

Here's my code from my .aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" EnableEventValidation="false" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Cascading DropDown</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>   
            <asp:DropDownList ID="ContainerddlSubCategory" AutoPostBack="false" runat="server" Visible="false"></asp:DropDownList>
                <asp:UpdatePanel ID="countrypanel" runat="server">
                    <ContentTemplate>
                        <asp:DropDownList ID="ddlCategory1" DataSourceID="sourceCountries" DataTextField="Name" DataValueField="ID" CssClass="form-control" AutoPostBack="true" AppendDataBoundItems="true"
                            runat="server"
                            OnSelectedIndexChanged="ddlCategory1_SelectedIndexChanged">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="sourceCountries" runat="server" ConnectionString="<%$ ConnectionStrings:DBCS %>" SelectCommand="SELECT Name, ID FROM Countries ORDER BY Name"/>                
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="ddlCategory1" />
                    </Triggers>
                </asp:UpdatePanel>


                <asp:DropDownList ID="ContainerddlCategory" runat="server" onchange="getSubCategory(this.value)" Visible="false"></asp:DropDownList>

                <asp:UpdatePanel ID="statepanel" runat="server">
                    <ContentTemplate>
                        <asp:DropDownList ID="ddlSubCategory1" AutoPostBack="true"
                            AppendDataBoundItems="true" runat="server" CssClass="form-control">
                        </asp:DropDownList>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="ddlSubCategory1" />
                    </Triggers>
                </asp:UpdatePanel>        </div>
    </form>   
</body>
</html>

aspx.cs

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

public partial class _Default : System.Web.UI.Page
{
private string connectionString = WebConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void ddlCategory1_SelectedIndexChanged(object sender, EventArgs e)
{
    String strQuery = "SELECT ID AS SubCategory, State AS SubCategoryName from States where Country=@CountryID" + ddlCategory1.SelectedValue;
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strQuery;
            cmd.Connection = con;
            con.Open();
            ddlSubCategory1.Items.Clear();
            ddlSubCategory1.DataSource = cmd.ExecuteReader();
            ddlSubCategory1.DataTextField = "SubCategoryName";
            ddlSubCategory1.DataValueField = "SubCategoryID";
            ddlSubCategory1.DataBind();
            ddlSubCategory1.Items.Insert(0, new ListItem("Select SubCategory", "-1"));
            con.Close();
        }
    }
}
}

aspx page code

                <asp:DropDownList ID="ContainerddlSubCategory" AutoPostBack="false" runat="server" Visible="false"></asp:DropDownList>
                    <asp:UpdatePanel ID="countrypanel" runat="server">
                        <ContentTemplate>
                            <asp:DropDownList ID="ddlCategory1" CssClass="form-control" AutoPostBack="true" AppendDataBoundItems="true"
                                runat="server"
                                OnSelectedIndexChanged="ddlCategory1_SelectedIndexChanged">
                            </asp:DropDownList>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="ddlCategory1" />
                        </Triggers>
                    </asp:UpdatePanel>


                    <asp:DropDownList ID="ContainerddlCategory" runat="server" onchange="getSubCategory(this.value)" Visible="false"></asp:DropDownList>

                    <asp:UpdatePanel ID="statepanel" runat="server">
                        <ContentTemplate>
                            <asp:DropDownList ID="ddlSubCategory1" AutoPostBack="true"
                                AppendDataBoundItems="true" runat="server" CssClass="form-control">
                            </asp:DropDownList>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="ddlSubCategory1" />
                        </Triggers>
                    </asp:UpdatePanel>

aspx.cs page code

protected void ddlCategory1_SelectedIndexChanged(object sender, EventArgs e)
{
    String strQuery = "select SubCategoryID,SubCategoryName from SubCategoryMast where CategoryID="+ddlCategory1.SelectedValue;
    using (SqlConnection con = new SqlConnection(strcon))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strQuery;
            cmd.Connection = con;
            con.Open();
            ddlSubCategory1.Items.Clear();
            ddlSubCategory1.DataSource = cmd.ExecuteReader();
            ddlSubCategory1.DataTextField = "SubCategoryName";
            ddlSubCategory1.DataValueField = "SubCategoryID";
            ddlSubCategory1.DataBind();
            ddlSubCategory1.Items.Insert(0, new ListItem("Select SubCategory", "-1"));
            con.Close();
        }
    }
}

Try this one its work

I figured it out my initial problem. Instead of I need to use . The AjaxToolkit components won't work with out it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM