如何要连接一个DropDownList使AJAX调用到服务器?服务器、DropDownList、AJAX

2023-09-10 20:42:38 作者:墨閼

我有我的网页的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