asp.net JQuery Ajax WebService 实现 级联 Dropdwonlist

新建WebService

[System.Web.Script.Services.ScriptService]//前台调用方法需要加入这句话
[WebMethod]
    //[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<GWInfo> GetGwName(string prefix)
    {
        List<GWInfo> persons = new List<GWInfo>();
        GWInfo a = new GWInfo();
        a.dep = prefix;
        foreach (GWInfo g in GW.GetGWByA(a))
        {
            persons.Add(g);
        }
        return persons; 
    }

前台,使用JQuery的Ajax调用WebService内方法

引用jq库

<script src="../JQuery/jquery-1.10.2.js" type="text/javascript"></script>

<script type="text/javascript">
            $(function () {
                $("#<%= DropDownList1.ClientID %>").change(function () {
                    //                    $.getJSON(
                    //                "JsonText.txt",
                    //                 function (JsonData) {
                    //                     $.each(JsonData.employees, function (i, item) {
                    //                         $("<option value='" + item.firstName + "'>" + item.firstName + "</option>").appendTo("#<%= DropDownList2.ClientID %>");
                    //                     })
                    //                 })
                    $.ajax({
                        url: '<%=ResolveUrl("~/Test/GetAuto.asmx/GetGwName") %>',
                        data: "{ 'prefix': '" + $(this).val() + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (JsonData) {
                            $("#<%= DropDownList2.ClientID %>").empty();
                            $(JsonData.d).each(function () {
                                $("<option value='" + this.name + "'>" + this.name + "</option>").appendTo("#<%= DropDownList2.ClientID %>");
                            })
                        }
                    });
                });
            });  
    </script>  

注释的方法是Ajax调用Json,JsonText.txt文本内容(Json格式)

{  
"employees": [  
{ "firstName":"Bill" , "lastName":"Gates" },  
{ "firstName":"George" , "lastName":"Bush" },  
{ "firstName":"Thomas" , "lastName":"Carter" }  
]  
} 


<div>
    部门:<asp:DropDownList runat ="server" ID ="DropDownList1" Width ="120px">
    <asp:ListItem Value ="">--选择--</asp:ListItem>
    <asp:ListItem Value ="IT">IT</asp:ListItem>
    <asp:ListItem Value ="财务">财务</asp:ListItem>
    </asp:DropDownList>
    <asp:DropDownList runat ="server" ID ="DropDownList2" Width ="120px"></asp:DropDownList>
    </div>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 AJAX 调用 ASP.NET 后台方法可以通过以下步骤实现: 1. 在 ASP.NET Web 应用程序中添加一个 Web 服务 (Web Service),或者一个 Web API 控制器 (Web API Controller)。 2. 在 Web 服务或 Web API 控制器中创建一个可供 AJAX 调用的方法。 3. 在 ASP.NET 页面中添加一个 ScriptManager 控件,并启用 ASP.NET AJAX。 4. 在页面中使用 JavaScript/jQuery 编写 AJAX 调用,将请求发送到 Web 服务或 Web API 控制器中的方法。 下面是一个简单的示例: 1. 创建一个 Web 服务或 Web API 控制器 ```csharp // Web 服务 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] public class MyWebService : System.Web.Services.WebService { [WebMethod] public string HelloWorld(string name) { return "Hello " + name + "!"; } } // Web API 控制器 public class MyApiController : ApiController { [HttpGet] public string HelloWorld(string name) { return "Hello " + name + "!"; } } ``` 2. 在 ASP.NET 页面中添加 ScriptManager 控件,并启用 ASP.NET AJAX ```html <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyNamespace.MyPage" %> <!DOCTYPE html> <html> <head runat="server"> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <asp:ScriptManager runat="server" EnablePartialRendering="true" /> </head> <body> <form id="form1" runat="server"> <div> <input type="text" id="name" /> <button id="helloButton">Say hello</button> <div id="result"></div> </div> </form> <script type="text/javascript"> $(document).ready(function () { $('#helloButton').click(function () { var name = $('#name').val(); $.ajax({ type: "POST", url: "MyWebService.asmx/HelloWorld", // 或者 "api/MyApi/HelloWorld" data: JSON.stringify({ name: name }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { $('#result').text(response.d); }, error: function (xhr, status, error) { console.log(xhr.responseText); } }); }); }); </script> </body> </html> ``` 在上面的示例中,我们使用了 jQuery 库来简化 AJAX 调用,通过给按钮添加一个 click 事件来触发 AJAX 请求,将用户输入的姓名作为参数传递给后台方法,并将返回结果显示在页面上的一个 div 元素中。 ASP.NET Web 应用程序中的 Web 服务和 Web API 控制器都可以使用这种方式来实现 AJAX 调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值