每当我必须处理许多下拉列表(ddl)时,我就会在脑海中产生这种困惑。我的问题实际上是有任何最佳实践或内置功能或一些解决这种情况的工作。我在下面给出了一个场景作为测试用例。假设我有4 ddl,这取决于之前的选择。
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应用程序
提前致谢。
答案 0 :(得分: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事件添加相同的函数(在本例中为Filter)
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
...