首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ajax调用后Jquery ajax ServerControl DropDownlist选定项

在Ajax调用后Jquery ajax ServerControl DropDownlist选定项
EN

Stack Overflow用户
提问于 2012-03-24 05:37:43
回答 1查看 1.3K关注 0票数 0

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

代码语言:javascript
复制
 <%@ 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>

和表单

代码语言:javascript
复制
    <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>

和后面的代码

代码语言:javascript
复制
 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
      }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-24 05:42:34

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9846786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档