我已经好几天没有运气了。我只是想让一对Cascading DropDownList工作 - 这就是它。但是,DropDownLists甚至不会显示" PromptText"文字,它肯定不会填充。
我已经从网络上复制并粘贴了几个示例,我遇到了同样的问题:空的下拉列表。没有错误消息。我做错了什么?
我的服务器正在运行IIS:7.0,ASP.Net运行时版本:4.0 / 4.5
我所关注的最新教程位于here
这是我的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();
}
}
这是我的.aspx页面中的代码
<%@ 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();
}
}
}
}
答案 0 :(得分:0)
<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>
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();
}
}
}
尝试这个工作
答案 1 :(得分:0)
我想出了我最初的问题。而不是我需要使用。 AjaxToolkit组件无法使用它。