如何要连接一个DropDownList使AJAX调用到服务器? [英] How to wire up a DropDownList to make AJAX calls to the server?

查看:86
本文介绍了如何要连接一个DropDownList使AJAX调用到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网页的ASP.NET 的DropDownList 服务器控件 Default.aspx的。我试图处理选择更改事件(如果使用的服务器事件,这是的SelectedIndexChanged 事件)客户端,使一个AJAX调用回服务器(我用的UpdatePanel几年前,但希望只使用的jQuery / AJAX 这样做了)。

I have an ASP.NET DropDownList server control on my page Default.aspx. I'm trying to handle the the selection change event (if using server events it was SelectedIndexChanged event) client side and make an AJAX call back to the server (I used to use UpdatePanels a few years back but want to just use jQuery/ajax to do this now).

我有以下简单的方法服务器端(下面这个例子中的 http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ ):

I have the following simple method server side (following this example http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/):

[WebMethod]
public static void MyDDL_SelectedIndexChanged()
{
  //Do some processing
}

然后,我有这个页面中的JavaScript:

I then have this JavaScript in the page:

<script type="text/javascript">
      $(document).ready(function () {
         // Add the page method call as an onclick handler for the control.
         $("<%= MyDDL.ClientID %>").click(function () {
               $.ajax({
                  type: "POST",
                  url: "Default.aspx/MyDDL_SelectedIndexChanged",
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json"
               });
            });
            });
</script>

下面是服务器控件:

<asp:DropDownList ID="MyDDL" runat="server" Width="340px" />

由于它代表这不起作用。没有错误或任何东西。该方法不会被调用,不管我做什么与我的互动的DropDownList

我相信这个问题是,点击 JavaScript事件是不正确的要连接到控制。我也试过变更,并没有擦出火花。基本上我想要的是在选择发生了变化的控制,脚本被称为这反过来又要求我的服务器端方法。

I believe the issue is that the click JavaScript event is not the correct one to wire up to the control. I also tried change and that didn't work either. Basically what I want is when the selection has changed on the control, the script is called which in turn calls my server side method.

任何人能帮助什么我可能做不正确?

Can anyone assist on what I may be doing incorrectly?

推荐答案

尝试改变事件,而不是的点击

Try the change event instead of the click

这篇关于如何要连接一个DropDownList使AJAX调用到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆