简体   繁体   English

如何通过使用C#中的级联下拉菜单选择单选按钮列表中的单选按钮来更改下拉列表值

[英]how to change dropdownlist values by selecting radio button in the radiobuttonlist using cascading drop down in C#

I have one Radio buttonlist with two radio buttons and one dropdownlist in my asp.net web application. 我的asp.net Web应用程序中有一个带有两个单选按钮和一个下拉列表的单选按钮列表。 I need to change the drop down list values with respect to selecting radio buttons in client side using Ajax Cascading drop down. 我需要使用Ajax级联下拉菜单来更改在客户端选择单选按钮的下拉列表值。 Can any one provide solutions for that means it will really helpful for my project. 任何人都可以为此提供解决方案,这对我的项目确实有帮助。

Thank you... 谢谢...

I have a ready code for a similar requirement. 我已经准备好了类似要求的代码。 i hope it helps. 我希望这会有所帮助。

Solution : Let's say there is a xml file with states. 解决方案:假设有一个带有状态的xml文件。 This is your data source for drop down list which you need to populate on radio button click. 这是下拉列表的数据源,您需要在单击单选按钮时填充该列表。

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

Aspx code Aspx代码

<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"/>

Jquery code to call a web method jQuery代码调用Web方法

   <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 in the same page code behind WebMethod在同一页代码后面

    [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;
    }

State Class 州级

   public class State
   { 
        public string Name { get; set; }
        public string value { get; set; }
        public string Abbreviation { get; set; }
   }

Explaination 讲解

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.

I assumed that you are familiar with Jquery. 我假设您熟悉Jquery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM