AjaxToolkit初使用之坎坷经历(含全国省市区xml)

本文介绍了一个使用AjaxControlToolkit实现的三级联动菜单案例,并解决了在实施过程中遇到的“方法错误500”的问题。文章提供了完整的页面代码、后台逻辑及WebService实现方案。

前段时间一直在看老赵的ajax的webcast

今天正好说是要做一个三级联动的菜单,想到ajaxToolkit中有这么个东西

就把文档一开,开始写了

结果总是出现 “方法错误 500”

 网上搜索之后,发现是web.config的问题

解决方法是创建一个基于ajax的项目。然后对比一下web.config

增加其中没有的内容

这么个问题搞了1个多小时

本以为简单易用的ajaxToolkit,要入门看来还是要个过程的

ContractedBlock.gifExpandedBlockStart.gif页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"  EnableEventValidation="false" Inherits="AJAXEnabledWebApplication1._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!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>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
       
<ajaxToolkit:ToolkitScriptManager EnablePartialRendering="true" runat="server" ID="ScriptManager1" />
    
<div class="demoarea">
        
<div class="demoheading">CascadingDropDown Demonstration</div>
        
<table>
            
<tr>
                
<td></td>
                
<td><asp:DropDownList ID="DropDownList1" runat="server" Width="170" /></td>
            
</tr>
            
<tr>
                
<td></td>
                
<td><asp:DropDownList ID="DropDownList2" runat="server" Width="170" /></td>
            
</tr>
            
<tr>
                
<td>区县</td>
                
<td><asp:DropDownList ID="DropDownList3" runat="server" Width="170" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" /></td>
            
</tr>
        
</table>
        
<br />
        
        
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
            Category
="province"  PromptText="请选择"  LoadingText="[Loading ]"
            ServicePath
="WebServicePCD.asmx" ServiceMethod="GetproviceNames" />
        
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
            Category
="city" ServicePath="WebServicePCD.asmx" PromptText="请选择" LoadingText="[Loading ]"
            ServiceMethod
="GetcityNames" ParentControlID="DropDownList1" />
        
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
            Category
="country" PromptText="请选择" LoadingText="[Loading ]"
            ServicePath
="WebServicePCD.asmx" ServiceMethod="GetcountryNames"
            ParentControlID
="DropDownList2" />
      
       
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="inline">
            
<ContentTemplate>
                
<asp:Label ID="Label1" runat="server" Text="请选择" />
            
</ContentTemplate>
            
<Triggers>
                
<asp:AsyncPostBackTrigger ControlID="DropDownList3" EventName="SelectedIndexChanged" />
            
</Triggers>
        
</asp:UpdatePanel>
    
</div>
    
</form>
</body>
</html>

 

ContractedBlock.gif页面后台
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;

namespace AJAXEnabledWebApplication1
{
    
public partial class _Default : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }
        
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            
// Get selected values
            string make = DropDownList1.SelectedItem.Text;
            
string model = DropDownList2.SelectedItem.Text;
            
string color = DropDownList3.SelectedItem.Text;

            
// Output result string based on which values are specified
            if (string.IsNullOrEmpty(make))
            {
                Label1.Text 
= "请选择省";
            }
            
else if (string.IsNullOrEmpty(model))
            {
                Label1.Text 
= "请选择城市";
            }
            
else if (string.IsNullOrEmpty(color))
            {
                Label1.Text 
= "请选择区县";
            }
            
else
            {
                Label1.Text 
= string.Format("您选择了 {0} {1} {2}. !", model,make,color );
            }
        }
    }
}

 

ContractedBlock.gifExpandedBlockStart.gifWebService
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Xml;
using AjaxControlToolkit;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Script.Services;
namespace AJAXEnabledWebApplication1
{
    
/// <summary>
    
/// WebServicePCD 的摘要说明
    
/// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(
false)]
    [System.Web.Script.Services.ScriptService]
    
public class WebServicePCD : System.Web.Services.WebService
    {

        
// Member variables
        private static XmlDocument _document;
        
private static object _lock = new object();

        
// we make these public statics just so we can call them from externally for the
        
// page method call
        public static XmlDocument Document
        {
            
get
            {
                
lock (_lock)
                {
                    
if (_document == null)
                    {
                        
// Read XML data from disk
                        _document = new XmlDocument();
                        _document.Load(HttpContext.Current.Server.MapPath(
"Address.xml"));
                    }
                }
                
return _document;
            }
        }
        
public static string[] Hierarchy
        {
            
get { return new string[] { "province""city" }; }
        }

        
/// <summary>
        
/// Constructor to initialize members
        
/// </summary>
        public WebServicePCD()
        {
        }

        
/// <summary>
        
/// Helper web service method
        
/// </summary>
        
/// <param name="knownCategoryValues">private storage format string</param>
        
/// <param name="category">category of DropDownList to populate</param>
        
/// <returns>list of content items</returns>
        [WebMethod]
        
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
        {
            
// Get a dictionary of known category/value pairs
            StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

            
// Perform a simple query against the data document
            return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);
        }

        [WebMethod]
        
public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
        {
            
return new WebServicePCD().GetDropDownContents(knownCategoryValues, category);
        }

        [WebMethod]
        
public CascadingDropDownNameValue[] GetproviceNames(string knownCategoryValues, string category)
        {
            StringDictionary knownCategoryValuesDictionary 
= AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            XmlNodeList xnl 
=Document.SelectNodes("//province/@name");
            List
<CascadingDropDownNameValue> value = new List<CascadingDropDownNameValue>();
            
foreach (XmlNode xn in xnl)
            {
                value.Add(
new CascadingDropDownNameValue(xn.Value,xn.Value));
            }
            
return value.ToArray();
        }


        [WebMethod]
        
public CascadingDropDownNameValue[] GetcityNames(string knownCategoryValues, string category)
        {
            StringDictionary kcv 
= AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            
if (!kcv.ContainsKey("province"))
            {
                
return null;
            }
            XmlNodeList xnl 
= Document.SelectNodes("//province[@name=\"" + kcv["province"] + "\"]/*/@name");
            List
<CascadingDropDownNameValue> value = new List<CascadingDropDownNameValue>();
            
foreach (XmlNode xn in xnl)
            {
                value.Add(
new CascadingDropDownNameValue(xn.Value, xn.Value));
            }
            
return value.ToArray();
        }

        [WebMethod]
        
public CascadingDropDownNameValue[] GetcountryNames(string knownCategoryValues, string category)
        {
            StringDictionary kcv 
= AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            
if (!kcv.ContainsKey("city"))
            {
                
return null;
            }
            XmlNodeList xnl 
= Document.SelectNodes("//city[@name=\"" + kcv["city"] + "\"]/*/@name");
            List
<CascadingDropDownNameValue> value = new List<CascadingDropDownNameValue>();
            
foreach (XmlNode xn in xnl)
            {
                value.Add(
new CascadingDropDownNameValue(xn.Value, xn.Value));
            }
            
return value.ToArray();
        }
    }
}

 

 下面再传一个这个三级联动菜单的全国省市区的xml

 http://files.cnblogs.com/billmo/Address.rar

 

转载于:https://www.cnblogs.com/billmo/archive/2008/12/15/1355576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值