Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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
C# 如何使用C中的级联下拉菜单在radiobuttonlist中选择单选按钮来更改dropdownlist值_C#_Asp.net_Ajax_Drop Down Menu_Radiobuttonlist - Fatal编程技术网

C# 如何使用C中的级联下拉菜单在radiobuttonlist中选择单选按钮来更改dropdownlist值

C# 如何使用C中的级联下拉菜单在radiobuttonlist中选择单选按钮来更改dropdownlist值,c#,asp.net,ajax,drop-down-menu,radiobuttonlist,C#,Asp.net,Ajax,Drop Down Menu,Radiobuttonlist,我在asp.net web应用程序中有一个单选按钮列表,其中有两个单选按钮和一个下拉列表。我需要更改有关使用Ajax级联下拉列表在客户端选择单选按钮的下拉列表值。任何一个能提供解决方案,这意味着它将真正有助于我的项目 谢谢…我有一个类似需求的现成代码。我希望有帮助 解决方案:假设有一个包含状态的xml文件。这是您需要在单选按钮单击时填充的下拉列表的数据源 <?xml version="1.0" encoding="utf-8" ?> <states> <sta

我在asp.net web应用程序中有一个单选按钮列表,其中有两个单选按钮和一个下拉列表。我需要更改有关使用Ajax级联下拉列表在客户端选择单选按钮的下拉列表值。任何一个能提供解决方案,这意味着它将真正有助于我的项目


谢谢…

我有一个类似需求的现成代码。我希望有帮助

解决方案:假设有一个包含状态的xml文件。这是您需要在单选按钮单击时填充的下拉列表的数据源

 <?xml version="1.0" encoding="utf-8" ?>
 <states>
 <state name="ALABAMA" abbreviation="AL" />
 <state name="ALASKA" abbreviation="AK" />
 </states>  
解释


我假设您熟悉Jquery。

一定要提高您的帐户比率抱歉,我需要使用级联下拉列表。可以使用层叠下拉菜单吗?好的。然后你可以试试这个链接。并尝试将级联下拉列表的ControlId更改为单选按钮列表。我不确定它是否是那样工作的。如果它不起作用,那么欢迎来到Jquery的世界。
<asp:RadioButtonList CssClass="radio" runat="server" ID="rblist">
    <asp:ListItem  Value="1">Yes</asp:ListItem>
    <asp:ListItem Value="2">No</asp:ListItem>
</asp:RadioButtonList>
<br/>
<asp:DropDownList runat="server" ID="ddlStates"/>
   <script type="text/javascript">

        $(document).ready(function () { 

            $("input:radio[name='rblist']").click(function () {

                var selectedRadio = $("input:radio[name='rblist']:checked").val();

                //Code to fetch complex datatype
                $.ajax({
                    type: "POST",
                    url: "/Samples.aspx/GetStatesWithAbbr",
                    dataType: "json",
                    data: "{ id :'" + selectedRadio + "'}",
                    contentType: "application/json; charset=utf-8",
                    success: function (msg) {
                        //alert(msg.d);
                        $("#ddlStates").get(0).options.length = 0;
                        $("#ddlStates").get(0).options[0] = new Option("-- Select state --", "-1");

                        $.each(msg.d, function (index, item) {
                            $("#ddlStates").get(0).options[$("#ddlStates").get(0).options.length] = new Option(item.Name, item.Abbreviation);
                        });
                    },
                    error: function () {
                        alert('error');
                    }
                });

            });

            $("#ddlStates").bind("change", function () {
                $('#' + '<%= lblSelectedState.ClientID %>').val($(this).val());
            });
        });
    </script>
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static List<State> GetStatesWithAbbr(string id)
    {
        List<State> sbStates = new List<State>();

        XmlDocument doc = new XmlDocument();
        string filePath = HttpContext.Current.Server.MapPath("~/App_Data/States.xml");
        doc.Load(filePath);

        try
        {
            foreach (XmlElement xnl in doc.DocumentElement.ChildNodes)
            {
                State st = new State();
                st.Name = xnl.Attributes["name"].Value;
                st.Abbreviation = xnl.Attributes["abbreviation"].Value;
                st.value = xnl.Attributes["name"].Value;
                sbStates.Add(st);
            }
        }
        catch (Exception ex)
        {
            string exp = ex.ToString(); //Setup a breakpoint here to verify any exceptions raised.
        }
        return sbStates;
    }
   public class State
   { 
        public string Name { get; set; }
        public string value { get; set; }
        public string Abbreviation { get; set; }
   }
1. On click of radio button we call a web method defined in code
behind.
2. web method accepts radio button's selected value and returns the states.
3. State is a complex type.Returned type is json.
4. On Success returned data is populated in dropdownlist.