ASP.NET / C#错误 - 通过ajax从下拉列表传递值

时间:2015-03-31 14:51:19

标签: javascript c# jquery asp.net ajax

我正在尝试使用JQuery AJAX链接两个下拉列表,但我没有得到任何东西。

下拉列表的ASP.NET代码是:

<td><a href="#" title="Choose the park that you would like. Mandatory Field.">Park*</a></td><!-- PARK -->
<td>
    <asp:DropDownList class="form-control" ID="parkDDL" ClientIDMode="Static" onchange="ShowCurrentBuilding()" style="width:150px;" runat="server">
    <asp:ListItem text="ALL" value="ALL"></asp:ListItem>
    <asp:ListItem text="Central" value="C"></asp:ListItem>
    <asp:ListItem text="West" value="W"></asp:ListItem>
    <asp:ListItem text="East" value="E"></asp:ListItem>
    </asp:DropDownList>
</td>

<td><a href="#" title="Choose the building that you would like">Building</a>     </td><!-- BUILDING -->
<td>
    <asp:DropDownList class="form-control" AutoPostBack="true" ID="buildingDDL" runat="server" style="width:300px;" OnSelectedIndexChanged="buildingToRoom"></asp:DropDownList>
</td>

我在JS标签之间的JQuery代码(正如你所看到的,我不太确定如何输出返回到下拉列表的内容):

function ShowCurrentBuilding() {
$.ajax(
{
type: "POST",
url: "CreateRequest.aspx/GetCurrentBuilding",
data: '{name: ' + $('#<%= parkDDL.ClientID %> option:selected').val() + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}

function OnSuccess(response) {
alert(response.d);
}

我还尝试了以下文档就绪功能:

$(document).ready(function () {
$('#<%=parkDDL.ClientID %>').on("change",function ShowCurrentBuilding() {
            $.ajax(
            {
                type: "POST",
                url: "CreateRequest.aspx/GetCurrentBuilding",
                data: '{name: "' + $(this).find("option:selected").val() + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccessBuilding,
                failure: function (response) {
                    alert(response.d);
                }
            });
        });

        function OnSuccessBuilding(response) {
            alert(response.d);
        }

});

最后,这是我的C#代码:

namespace Team11
{
  public partial class CreateRequest : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {

//populate my initial drop down lists etc

}
}

//here is the function that is called in the JQuery function

[System.Web.Services.WebMethod]

    public string GetCurrentBuilding(string name)
    {
        string textbx = "";
        textbx = name;

        return textbx;


    }

我只想通过ajax过滤下拉列表,这样页面就不会重新加载AutoPostBack =&#34; true&#34;。

提前感谢任何有帮助的人!!

1 个答案:

答案 0 :(得分:0)

使用客户端功能onchange而不是服务器端OnSelectedIndexChanged。