Ajax级联下拉列表不适用于webservices方法

时间:2015-05-22 12:49:18

标签: asp.net web-services c#-4.0 ajaxcontroltoolkit

需要从另一个下拉值绑定下拉值,而不是Web方法 即使在添加了Web引用后也没有显示任何错误。

我收到错误*方法500.

我正在使用visual studio,我将asp.net应用程序作为一个项目,将web服务作为另一个项目。我在我的asp.net应用程序中使用Web服务。

我的网络服务代码存在某种问题。但我无法从asp.net应用程序连续调试到Web服务。

我在应用程序和Web服务中都设置了断点,但是在Web服务中没有激活断点,它显示了连接错误。

如何在localhost上托管时这样做?

my country.aspx code

<form id="form1" runat="server">
    <ajax:ToolkitScriptManager EnablePageMethods="true" ID="tsmcascading" runat="server">
    </ajax:ToolkitScriptManager>
    <div>
        <div>
            <asp:DropDownList ID="ddlstate" runat="server" AutoPostBack="true">
            </asp:DropDownList>
            <ajax:CascadingDropDown ID="cddstate" runat="server" ServicePath="~/cascadingdropdown.asmx"
                Category="stateid" ServiceMethod="addstate" TargetControlID="ddlstate" PromptText="select state"
                LoadingText="Loading...">
            </ajax:CascadingDropDown>
        </div>
    </div>
    <div>
        <div>
            <asp:DropDownList ID="ddlcity" runat="server">
            </asp:DropDownList>
            <ajax:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="ddlcity"
                Category="stateid" ParentControlID="ddlstate" ServiceMethod="addcity" PromptText="select city"
                ServicePath="~/cascadingdropdown.asmx" LoadingText="Loading...">
            </ajax:CascadingDropDown>
        </div>
    </div>
</form>

我的网络服务代码(.asmx)

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

/// <summary>
/// Summary description for cascadingdropdown
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]

// 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 cascadingdropdown : System.Web.Services.WebService
{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["addcontact"].ToString());
    public cascadingdropdown()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    public CascadingDropDownNameValue[] addstate(string state, string city)
    {
        con.Open();
        SqlCommand cmdstate = new SqlCommand("select stateid,statename from tblStates", con);
        cmdstate.ExecuteNonQuery();
        SqlDataAdapter dastate = new SqlDataAdapter(cmdstate);
        DataSet dsstate = new DataSet();
        dastate.Fill(dsstate);
        con.Close();
        List<CascadingDropDownNameValue> statesnames = new List<CascadingDropDownNameValue>();
        foreach (DataRow dtrow in dsstate.Tables[0].Rows)
        {
            string stateid = dtrow["stateid"].ToString();
            string statename = dtrow["statename"].ToString();
            statesnames.Add(new CascadingDropDownNameValue(statename, stateid));
        }
        return statesnames.ToArray();

    }

    [WebMethod]
    public CascadingDropDownNameValue[] addcity(string state, string city)
    {
        int stateid;
        StringDictionary statedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(state);
        stateid = Convert.ToInt32(statedetails["state"]);
        con.Open();
        SqlCommand cmdcity = new SqlCommand("select cityid,stateid,cityname from tblcities where stateid=@stateid", con);
        cmdcity.Parameters.AddWithValue("@stateid", stateid);
        cmdcity.ExecuteNonQuery();
        SqlDataAdapter dacity = new SqlDataAdapter(cmdcity);
        DataSet dscity = new DataSet();
        dacity.Fill(dscity);
        con.Close();
        List<CascadingDropDownNameValue> citynames = new List<CascadingDropDownNameValue>();
        foreach (DataRow dtrow in dscity.Tables[0].Rows)
        {
            string cityid = dtrow["cityid"].ToString();
            string cityname = dtrow["cityname"].ToString();
            citynames.Add(new CascadingDropDownNameValue(cityid, cityname));
        }
        return citynames.ToArray();
    }

}`

2 个答案:

答案 0 :(得分:0)

尝试使用AjaxControltoolkit级联下拉列表使用的函数的标准定义:

public CascadingDropDownNameValue[] addState(String knownCategoryValues, String category, String contextKey)
{
...
}
public CascadingDropDownNameValue[] addCity(String knownCategoryValues, String category, String contextKey)
{
...
}

上下文密钥不是必需参数,如果不需要,可以省略它。

答案 1 :(得分:0)

我有完全相同的问题,我找到了一个“教程”,我试图用我自己的代码使它,但我无法让它工作我也得到方法500错误。

尝试使用100%的网站代码

http://www.aspdotnet-suresh.com/2011/01/introduction-here-i-will-explain-how-to.html

它对我有用,我一次只修改了一些代码。