2012-07-13 23 views
1

我有2個下拉菜單,並首次下降正常工作,並從數據庫顯示的數據,但第二個下拉顯示這裏誤差500是我的web服務代碼:在阿賈克斯與websevice級聯下拉列表的第二個下拉顯示方法錯誤500

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


[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// 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 
{ 
private static string strconnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); 
//private static string strconnection = ConfigurationManager.AppSettings["ConnectionString"].ToString(); 
SqlConnection concategory = new SqlConnection(strconnection); 

public CascadingDropdown() 
{ 

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

[WebMethod] 
public CascadingDropDownNameValue[] BindCategoryDetails(string knownCategoryValues, string category) 
{ 

    concategory.Open(); 
    SqlCommand cmdcategory = new SqlCommand("select * from Categories", concategory); 
    //create list and add items in it by looping through dataset table 
    List<CascadingDropDownNameValue> categorydetails = new List<CascadingDropDownNameValue>(); 
    SqlDataReader drcategory= null ; 
    drcategory=cmdcategory.ExecuteReader(); 
    while(drcategory.Read()) 
    { 
     string CategoryID = drcategory ["categoryID"].ToString(); 
     string CategoryName = drcategory ["categoryName"].ToString(); 
     categorydetails.Add(new CascadingDropDownNameValue(CategoryName, CategoryID)); 
    } 
    concategory.Close(); 
    return categorydetails.ToArray(); 
} 
/// <summary> 
/// WebMethod to Populate State Dropdown 
/// </summary> 
[WebMethod] 
public CascadingDropDownNameValue[] BindProductDetails(string knownCategoryValues, string category) 
{ 
    int categoryID; 
    //This method will return a StringDictionary containing the name/value pairs of the currently selected values 
    StringDictionary categorydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); 
    categoryID = Convert.ToInt32(categorydetails["Category"]); 
    concategory.Open(); 
    SqlCommand cmdproduct = new SqlCommand("select * from Products where [email protected]", concategory); 
    cmdproduct.Parameters.AddWithValue("@categoryID", categoryID);   
    //create list and add items in it by looping through dataset table 
    List<CascadingDropDownNameValue> productdetails = new List<CascadingDropDownNameValue>();  
    SqlDataReader drproduct= null ; 
    drproduct=cmdproduct.ExecuteReader();  
    while(drproduct.Read())  { 
     string ProductID = drproduct ["categoryID"].ToString(); 
     string ProductName = drproduct ["categoryName"].ToString(); 
     productdetails.Add(new CascadingDropDownNameValue(ProductName, ProductID)); 
    } 
    concategory.Close();  
    return productdetails.ToArray(); 

} 

}

和ASPX代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false"%> 
<%@ Register Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" tagPrefix="ajax" %> 
<!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 runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 

</div> 

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<br /> 
<asp:DropDownList ID="ddlcategory" runat="server"></asp:DropDownList> 
<ajax:CascadingDropDown ID="ccdCategory" runat="server" Category="category" 
    TargetControlID="ddlcategory" PromptText="Select Category" 
    LoadingText="Loading Categories" ServiceMethod="BindCategoryDetails" 
    ServicePath="CascadingDropdown.asmx"> 
</ajax:CascadingDropDown> 
<asp:DropDownList ID="ddlproduct" runat="server"> 
</asp:DropDownList> 
<ajax:CascadingDropDown ID="ccdProduct" runat="server" Category="product" 
TargetControlID="ddlproduct" PromptText="Select Product" 
    LoadingText="Loading Products" ServiceMethod="BindProductDetails" 
    ScriptPath="CascadingDropdown.asmx"> 
    </ajax:CascadingDropDown> 
</form> 
</body> 
</html> 

如存在的其他任何ü需要知道的告訴我。謝謝。

+0

替換要分配類別ID來Projduct ID'串的ProductID = drproduct [ 「的categoryID」]。的ToString() ;'它不應該是ProductID? – 2012-07-13 15:00:11

回答

0

我想錯誤發生在bcz你正在分配CategoryID到ProductID。在第二下拉列表綁定代碼

string ProductID = drproduct ["categoryID"].ToString(); 
string ProductName = drproduct ["categoryName"].ToString(); 

上述代碼應通過該

string ProductID = drproduct ["ProductID"].ToString(); 
string ProductName = drproduct ["ProductName"].ToString(); 
在第二下拉
+0

我糾正了代碼,但問題仍然存在 – 2012-07-13 15:31:30

+0

檢查服務方法BindProductDetails返回數據。 – 2012-07-13 15:57:58