【问题标题】:Jquery Ajax ServerControl DropDownlist selected item after ajax callajax调用后的Jquery Ajax ServerControl DropDownlist选定项目
【发布时间】:2012-03-23 21:37:42
【问题描述】:

我该怎么做才能让下拉列表识别出它有一个选定的对象? 所以我得到了 JQUERY AJAX JSON 来填充服务器控件下拉列表。 用户在上述列表中选择一个选项。 然后单击 serverControl 按钮。 我收到“无效的回发或回调参数”。在尝试了一个小时左右“使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证”后,我放弃并在页面标记中设置 EnableEventValidation="false"。 现在不再有错误,但是当我检查 dropdownlist.selected 值时,它是空的。 我该怎么做才能让下拉列表识别它有一个选定的对象?

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Ajax.aspx.cs" Inherits="something" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" language="javascript">
           $().ready(function () {
                     $("#ddlCountry1").change(function () {
                         $.ajax({
                                type: "POST",
                                url: "Ajax.aspx/PopulateStates",
                                data: "{countryCode:" + "'" + bozo + "'" + "}",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (msg) {
                                       $("#ddlState").get(0).options.length = 0;
                                       $("#ddlState").get(0).options[0] = new Option("Select State", "-1");

                                       $.each(msg.d, function (value, item) {
                                              $("#ddlState").get(0).options[$("#ddlState").get(0).options.length] = new Option(item.Display, item.Value);
                                       });
                                       var itemCount = $("#ddlState").children().length;
                                       if (itemCount < 2) {
                                              $("#ddlState").hide();
                                       }
                                       else
                                       { $("#ddlState").show(); };
                                },
                                error: function () {
                                       alert("Failed to load states");
                                }
                         });
                  });
           });
    </script>

    </head>

和形式

    <body>
        <form id="form1" runat="server">
            <div>
               <table>
                   <tr><th>country</th><td>
                           <asp:DropDownList ID="ddlCountry1" runat="server" ClientIDMode="Static">
                           </asp:DropDownList>
                    </td></tr>
                <tr><th>state</th><td>
                        <asp:DropDownList ID="ddlState" runat="server" ClientIDMode="Static">
                           </asp:DropDownList>
                    </td></tr>
            </table>
        </div>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </form>
</body>

以及背后的代码

 protected void Page_Load(object sender, EventArgs e)
      {
             BindCountryDropDown();
      }

      private void BindCountryDropDown()
      {
             ddlCountry1.Items.Add(new ListItem("country1", "country1"));
             ddlCountry1.Items.Add(new ListItem("country2", "country2"));
             ddlCountry1.DataSource = managers.profileManager.GetCountries();
             ddlCountry1.DataTextField = "CountryName";
             ddlCountry1.DataValueField = "CountryAbbr";
             ddlCountry1.DataBind();
      }

      [WebMethod]
      public static ArrayList PopulateStates(string countryCode)
      {
             var p = managers.profileManager.GetStates(countryCode);
             ArrayList myStates = new ArrayList(p.ToArray());
             return myStates;
      }


      protected void Button1_Click(object sender, EventArgs e)
      {
             var dd = ddlState.SelectedValue; //is empty
      }

【问题讨论】:

    标签: json c#-4.0 jquery


    【解决方案1】:

    ASP.NET ID 不是客户端 ID。这是回发的内部 ID。您可以在生成的 ID 到达浏览器时查看它,也可以查看下拉菜单的 clientId 属性。

    【讨论】:

    • 所以我必须通过javascript来做?
    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多