Cascading Drop Down在Chrome和空白字段中显示“未定义”值

时间:2012-05-05 23:26:41

标签: asp.net sql cascadingdropdown

我正在开发一个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" %>

感谢大家的任何评论!

0 个答案:

没有答案