2014-03-06 45 views
1

每當我必須處理很多dropdownlist(ddl)時,我都會在腦海中產生混淆。我的問題實際上是否有任何最佳實踐或內置功能或一些解決此類情況的工作。我在下面給出了一個場景作爲測試用例。可以說我有4個ddl,它們依賴於前面的選擇。VB.NET:處理多個下拉列表


ddlContinent ddlCountry ddlCity ddlCurrency 
NorthAmerica USA   Mumbai Indian Rupee 
Europe   Canada  Colombo Sri Lanka Rupee 
Asia   England  Paris  USD 
[All]   India  London Candian Dollar 
       France  Chicago GBP 
       Sri Lanka Toronto 
       [All]  New Delhi 
          [All] 

Case1: If someone selects ddlContinent [All] and ddlCountry India; the ddlCity should be Mumbai and New Delhi Case2: If ddlContinent is Asia and ddlCountry is [All] then ddlCity should be Mumbai, New Delhi and Colombo

等等..

的噩夢是,我們需要編寫所有的整套的可能性可能的if-then條件。

最重要的是,當實際的最終輸出必須基於上述ddl選擇在表格對象中顯示時,我們必須再次編碼所有可能的if-then條件。

是否有捷徑。

注:以上是一個ASP.NET Web應用程序提前

感謝。

+1

谷歌這個:asp.net級聯dropdownlist –

+0

CascadingDropDown很酷,但我的要求是,我有多個父控制。具體而言,我有6個ddl,其中5個是相互依存的,最重要的是我必須給用戶[全部]選項。有沒有行業最佳做法?來吧!通常我們在求職網站上看到有這麼多的ddl,而且每個都有[All]選項。他們如何根據其他ddl的選擇填充每個ddl。這個領域的任何專家請.. –

回答

0

以及我解決它使用一個自定義類可能存在更好的方法,但在這裏我們去

規則

each ddl have one level 
top level affect to lower ddl 

工具

one stored procedure 

素描

public class DdlLevel 
{ 
    private DropDownList _ddl; 
    private IRepository _repository;  
    private int _level; 
    private string _displayField; 
    private string _valueField; 

    public DdlLevel(DropDownList ddl,int level, IRepository repository, string displayField, string valueField){ 
    _ddl = ddl; 
    _level = level; 
    _repository = repository; 
    _displayField = displayField; 
    _valueField = valueField; 
    } 

    public void Refresh(UserOptions userOptions){ 
    if userOptions.DdlLevelRaiseRefresh > _level){ 
    DataTable data = _repository.GetTablaFilter(userOptions) 
    _dll.DataSource = data; 
    _dll.DataTextField = _displayField; 
    _dll.DataValueField = _valueField; 
    _dll.DataBind(); 
    if (_dll.Item.Count > 0) _dll.SelectedIndex = 0; 
    } 
    } 
} 
在頁面

做一些如何

private IList<DdlLevel> _ddlRefresh; 

page_load(){ 
    _dllRefresh = DllLevelInit(); 
} 

IList<DdlLevel> DllLevelInit(){ 
    IList<DdlLevel> list = new List<DdlLevel>(); 
    list.add(new DdlLevel(firstdll,9,new yourRepository(...),"fieldtodisplay","fieldtostore"); 
    list.add(new DdlLevel(seconddll,8,new yourRepository(...),"fieldtodisplay","fieldtostore"); 
    ... 
    return list; 
} 
現在

每個DDL把相同的功能(在這種情況下濾波器)SelectedIndexChanged事件

protected void Filter(Object sender, EventArgs e){ 
    UserOptions options = GetCurrentValuesOfDll(sender); 
    for each DdlLevel ddl in _ddlRefresh 
     dld.Refresh(options); 
} 

private UserOptions GetCurrentValuesOfDll(Object sender){ 
UserOptions o = new UserOptions; 
    o.DdlLevelRaiseRefresh = GetLevelOfCurrentDdl(sender) 
    o.DdlCompany = null; 
    If Not (ddlCompany.SelectedValue = "All" Or ddlCompany.SelectedValue = "") Then 
     o.DdlCompany = CType(ddlCompany.SelectedValue, Integer) 
    End If 
    ... 
    return o; 
} 

private int GetLevelOfCurrentDdl(Object sender){ 
int level = 0; 
switch(sender.ID){ 
    case "ddlCompany"; 
     return 9; 
    ... 
} 
return level; 
} 

的SP是一些如何

create stored procedure DllFilterOnMyPage(useroption1, useroption2...) 
as 
set nocount on 

if (dllLevel > 8) select 'All' as DisplayField, -0 as ValueToStore union select * from yourtable 
if (dllLevel > 7) select 'All' as DisplayField, -0 as ValueToStore union select * from yourtable 
...