[英]VB.NET: dealing with multiple dropdownlist
I have this confusion in my mind whenever I have to deal with many dropdownlist (ddl). 每当我不得不处理许多dropdownlist(ddl)时,我都会感到困惑。 My question is actually is there any best practice or in-built functions or some work around to deal with such situation.
我的问题是实际上是否有最佳实践或内置功能,或者有一些工作可以解决这种情况。 I have given below a scenario as a test case.
我在下面给出了一个方案作为测试用例。 Lets say I have 4 ddl which are dependent upon the selection of the previous ones.
可以说我有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
And so on.. 等等..
The nightmare is that we need to code all the possible if-then conditions for the whole set of possibilities. 噩梦是我们需要为所有可能性编写所有可能的if-then条件。
Above all, when the actual final output has to be displayed in a Table Object based on the above ddl selections, there again we have to code all the possible if-then conditions. 最重要的是,当必须基于上述ddl选择将实际最终输出显示在表对象中时,我们再次必须对所有可能的if-then条件进行编码。
Is there a short-cut. 有捷径吗?
Note: The above is a ASP.NET Web Application 注意:以上是一个ASP.NET Web应用程序
Thanks in advance. 提前致谢。
well i resolved it using one custom class maybe exists better approach but here we go 好吧,我使用一个自定义类解决了它,也许存在更好的方法,但是在这里
Rules 规则
each ddl have one level
top level affect to lower ddl
Tools 工具类
one stored procedure
Sketch 草图
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;
}
}
}
in the page do some how 在页面中做一些如何
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;
}
now on each ddl put the same function(in this case Filter) for SelectedIndexChanged event 现在在每个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;
}
the sp is some how 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
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.