Ajax 调用自写的webservice 1


1、新建一个AJAX Control Toolkit Website
2、把CarsService.xml(相当一数据库)添加到项目的App_Data文件夹。
3、“添加新项”--“CarsService.asmx”,在CarsService.cs添加代码:
using System.Xml;

using System.Web.Services;
using System.Collections.Specialized;
using AjaxControlToolkit;
[System.Web.Script.Services.ScriptService()]
    private static XmlDocument _document;
    private static object _lock = new object();
-------
  public static XmlDocument Document
    {
       //code...
    }
-------
    public static string[] Hierarchy
    {
     //code...
    }

4、然后把原来的HelloWorld方法改为    GetDropDownContents()方法


5、在Default.aspx添加一个updatepanel,再在updatepanel添加三个Dropdownlist
、三个CascadingDropDown和一个Label,把Dropdownlist3的“启用AutoPastBack”选中。

6、在Default.aspx.cs添加代码:
    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string

knownCategoryValues, string category)
    {
        return new CarsService().GetDropDownContents(knownCategoryValues, category);
    }
------------
7、CascadingDropDown1的属性窗口,把TargetControlID设为"DropDownList1";
CascadingDropDown2的TargetControlID设为"DropDownList2";
CascadingDropDown3的TargetControlID设为"DropDownList3"

8、把DropDownList1的Category设为"Make",LoadingText设为"[Loading makes...]",
PromptText设为"Please select a make",ServiceMethod设为"GetDropDownContents",
ServicePath设为"CarsService.asmx" ;DropDownList2和DropDownList3的设置类似:
具体看代码:

        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server"

TargetControlID="DropDownList1"
            Category="Make"  PromptText="Please select a make"  LoadingText="[Loading makes...]"
            ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents" />
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server"

TargetControlID="DropDownList2"
            Category="Model" PromptText="Please select a model" LoadingText="[Loading models...]"
            ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server"

TargetControlID="DropDownList3"
            Category="Color" PromptText="Please select a color" LoadingText="[Loading colors...]"
            ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents"
            ParentControlID="DropDownList2" />
-------

9、在在Default.aspx.cs添加代码:
 protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        // code...
}

10、CarsService.cs的代码:

11、现在还不是开香槟的时候,因为当你选择了下拉框后Label却没有反馈东西,There Must Something Wrong!需要在Default.aspx的第一行添加代码:
 EnableEventValidation="false" 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值