<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ 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 runat="server"> <title></title> </head> <body style="height: 235px"> <form id="form1" runat="server"> <asp:ScriptManager ID="asm" runat="server" /> <div> Vendor: <asp:DropDownList ID="VendorsList" runat="server" /><br /> Contacts: <asp:DropDownList ID="ContactsList" runat="server" /><br /> </div> <CascadingDropDown ID="ccd1" runat="server" ServicePath="CascadingDropdown1.cs.asmx" ServiceMethod="GetVendors" TargetControlID="VendorsList" Category="VendorContact" PromptText="Select Vendor" /> <CascadingDropDown ID="ccd2" runat="server" ServicePath="CascadingDropdown1.cs.asmx" ServiceMethod="GetContactsForVendor" TargetControlID="ContactsList" ParentControlID="VendorsList" Category="Contact" PromptText="Select Contact" /> </form> </body> </html>
using System.Web.Script.Services; using AjaxControlToolkit; using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Collections.Generic; using System.Collections.Specialized; using System.Data.SqlClient; /// <summary> /// Summary description for WebService /// </summary> [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 WebService : System.Web.Services.WebService { [WebMethod] public CascadingDropDownNameValue[] GetContactsForVendor(string knownCategoryValues, string category) { int VendorID=1; StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); if (!kv.ContainsKey("Vendor") || !Int32.TryParse(kv["Vendor"], out VendorID)) { // throw new ArgumentException("Couldn't find vendor."); }; SqlConnection conn = new SqlConnection( "Data Source=KING-STARS;Initial Catalog=AdventureWorks;Integrated Security=True"); conn.Open(); SqlCommand comm = new SqlCommand( "SELECT Person.Contact.ContactID, FirstName, LastName FROM Person.Contact, Purchasing.VendorContact WHERE VendorID=@VendorID AND Person.Contact.ContactID=Purchasing.VendorContact.ContactID" , conn); comm.Parameters.AddWithValue("@VendorID", VendorID); SqlDataReader dr = comm.ExecuteReader(); List<CascadingDropDownNameValue> l = new List<CascadingDropDownNameValue>(); while (dr.Read()) { l.Add(new CascadingDropDownNameValue( dr["FirstName"].ToString() + " " + dr["LastName"].ToString(), dr["ContactID"].ToString())); } conn.Close(); return l.ToArray(); } [WebMethod] public CascadingDropDownNameValue[] GetVendors(string knownCategoryValues, string category) { SqlConnection conn = new SqlConnection( "Data Source=KING-STARS;Initial Catalog=AdventureWorks;Integrated Security=True"); conn.Open(); SqlCommand comm = new SqlCommand( "SELECT TOP 25 VendorID, Name FROM Purchasing.Vendor", conn); SqlDataReader dr = comm.ExecuteReader(); List<CascadingDropDownNameValue> l = new List<CascadingDropDownNameValue>(); while (dr.Read()) { l.Add(new CascadingDropDownNameValue( dr["Name"].ToString(), dr["VendorID"].ToString())); } conn.Close(); return l.ToArray(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)