Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Jquery Ajax ServerControl DropDownlist在Ajax调用后列出所选项目_Json_C# 4.0_Jquery - Fatal编程技术网

Json Jquery Ajax ServerControl DropDownlist在Ajax调用后列出所选项目

Json Jquery Ajax ServerControl DropDownlist在Ajax调用后列出所选项目,json,c#-4.0,jquery,Json,C# 4.0,Jquery,我如何让dropdownlist识别它有一个选定的对象? 因此,我使用jqueryajaxjson填充服务器控件dropdownlist。 用户在上述列表中选择一个选项。 然后单击服务器控制按钮。 我得到一个“无效回发或回调参数”。在尝试“使用ClientScriptManager.RegisterForEventValidation方法以注册回发或回调数据进行验证”一小时左右后,我放弃并在页面标记中设置EnableEventValidation=“false”。 现在没有更多的错误,但当我检查

我如何让dropdownlist识别它有一个选定的对象? 因此,我使用jqueryajaxjson填充服务器控件dropdownlist。 用户在上述列表中选择一个选项。 然后单击服务器控制按钮。 我得到一个“无效回发或回调参数”。在尝试“使用ClientScriptManager.RegisterForEventValidation方法以注册回发或回调数据进行验证”一小时左右后,我放弃并在页面标记中设置EnableEventValidation=“false”。 现在没有更多的错误,但当我检查dropdownlist.selected值时,它是空的。 我如何让dropdownlist识别它有一个选定的对象

 <%@ 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>

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

所以我必须通过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>
 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
      }