2012-02-08 87 views
0

我有一個無線buttonlist有兩個單選按鈕,並在我的asp.net web應用程序一個下拉列表中選擇RadioButtonList的單選按鈕更改下拉列表值。我需要關於更改下拉列表值使用Ajax級聯下拉在客戶端選擇單選按鈕。任何人都可以提供解決方案,這意味着它對我的項目真的很有幫助。如何使用級聯下拉在C#

謝謝...

+0

不增加你的accpetnace率 – 2012-02-08 13:23:46

回答

0

我有一個類似的需求準備好代碼。我希望它有幫助。

解決方案:比方說,有一個與狀態的XML文件。這是您的下拉列表的數據源,您需要在單選按鈕上單擊。

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

.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代碼調用在同一頁面代碼behi一個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第二

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

國家A級

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. 

我假定你所熟悉的jQuery的。

+0

對不起,我需要級聯下拉做。是否可以使用級聯下拉? – Kathirvel 2012-02-08 16:06:07

+0

好。那麼你可以試試這個鏈接。 http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Walkthrough/CCDWithDB.aspx,試着改變級聯下拉列表,單選按鈕列表控件ID。我不確定它是否以這種方式工作。如果它不起作用,那麼歡迎來到Jquery的世界。 – PraveenLearnsEveryday 2012-02-09 06:32:48