我正在开发一个 Web 表单,该表单使用 WebService 通过使用 Ajax Cascading DropDown 来填充 DropDownList 的数据。在 VS 中使用它自己的 IIS Express 实例对其进行调试时,它就像一个魅力。当我在 localhost 上安装 IIS 以执行完整测试时,下拉列表中填充了 Chrome 中的“未定义”值和 IE 中的空白字段。
我在下面提供了完整的代码。表单和 WebService 都驻留在名为 Query 的文件夹中。我检查了 CascadingDropDown 的路径。当我将其更改为其他任何内容时,该字段返回方法 500 错误。因此,我使用的路径是正确的,因为不会引发此错误。
在代码中,你会遇到“NoValidationDropDown”,请忽略这个,这不是错误的来源,只允许绕过验证。
我没有对 WebConfig 进行任何更改。如果这对 WebServices 的工作很重要,请告诉我。我已允许 TCP/IP 连接到我的 SQL Express 服务器数据库实例。
这是我的表单页面(Data_selector.aspx)的摘录:
<asp:Panel ID="Panel3" runat="server">
<p>
<b>Query Type</b><br />
<asp:NoValidationDropDown ID="ddlquery" runat="server"/>
<%--<asp:DropDownList ID="ddlquery" runat="server"/>--%>
<ajaxtoolkit:CascadingDropDown ID="ccdquery" runat="server" Category="QueryType" TargetControlID="ddlquery"
PromptText="Please select a query" LoadingText="Loading data" ServiceMethod="BindQueryType" ServicePath="~/Query/DropDownWebService.asmx"/>
</p>
<p>
<b>Specify query type</b><br />
<asp:NoValidationDropDown ID="ddlitem" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlitem_SelectedIndexChanged"/>
<%--<asp:DropDownList ID="ddlitem" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlitem_SelectedIndexChanged"/>--%>
<ajaxtoolkit:CascadingDropDown ID="ccditem" runat="server" Category="ItemType" TargetControlID="ddlitem" ParentControlID="ddlquery"
PromptText="Please specify query type" LoadingText="Loading data" ServiceMethod="BindItemType" ServicePath="~/Query/DropDownWebService.asmx"/>
</p>
<p>
这是我的 WebService.cs 文件的摘录:
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using AjaxControlToolkit;
using System.Web.Services;
using System;
using System.Text;
using System.Web.Script.Services;
using System.Web.Services.Protocols;
/// <summary>
/// Summary description for CascadingDropdown
/// </summary>
[WebService(Namespace = "NewWebApp")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class DropDownWebService : System.Web.Services.WebService
{
//Database connection string
private static string strconnection = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
//database connection
SqlConnection conquery = new SqlConnection(strconnection);
public DropDownWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
/// <summary>
/// WebMethod to Populate Query Dropdown
/// </summary>
[WebMethod]
public CascadingDropDownNameValue[] BindQueryType(string knownCategoryValues, string category)
{
conquery.Open();
SqlCommand cmdquery = new SqlCommand("SELECT * from QueryTypes", conquery);
cmdquery.ExecuteNonQuery();
SqlDataAdapter daquery = new SqlDataAdapter(cmdquery);
DataSet dsquery = new DataSet();
daquery.Fill(dsquery);
conquery.Close();
//create list and add items in it by looping through dataset table
List<CascadingDropDownNameValue> querydetails = new List<CascadingDropDownNameValue>();
foreach (DataRow dtrow in dsquery.Tables[0].Rows)
{
string Id = dtrow["Id"].ToString();
string Query = dtrow["Query"].ToString();
querydetails.Add(new CascadingDropDownNameValue(Query, Id));
}
return querydetails.ToArray();
}
/// <summary>
/// WebMethod to Populate Item Dropdown
/// </summary>
[WebMethod]
public CascadingDropDownNameValue[] BindItemType(string knownCategoryValues, string category)
{
//int Id;
//This method will return a StringDictionary containing the name/value pairs of the currently selected values
StringDictionary querydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
string Id = querydetails["QueryType"].ToString();
conquery.Open();
SqlCommand cmditem = new SqlCommand("SELECT Id, Item from ItemTypes where QueryId=@QueryId", conquery);
cmditem.Parameters.AddWithValue("@QueryId", Id);
cmditem.ExecuteNonQuery();
SqlDataAdapter daitem = new SqlDataAdapter(cmditem);
DataSet dsitem = new DataSet();
daitem.Fill(dsitem);
conquery.Close();
//create list and add items in it by looping through dataset table
List<CascadingDropDownNameValue> itemdetails = new List<CascadingDropDownNameValue>();
foreach (DataRow dtrow in dsitem.Tables[0].Rows)
{
string ItemId = dtrow["Id"].ToString();
string Item = dtrow["Item"].ToString();
itemdetails.Add(new CascadingDropDownNameValue(Item, ItemId));
}
return itemdetails.ToArray();
}
}
这是我的 WebService.asmx 文件:
<%@ WebService Language="C#" Class="DropDownWebService" CodeBehind="~/Query/DropDownWebService.cs" %>
感谢大家的任何评论!