簡體   English   中英

ASP.NET/C#錯誤-通過Ajax從下拉列表傳遞值

[英]ASP.NET/C# Error - passing value from drop down list via 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 =“ true”重新加載。

在此先感謝任何可以提供幫助的人!

使用客戶端功能onchange而不是服務器端OnSelectedIndexChanged。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM