【问题标题】:VB.NET: dealing with multiple dropdownlistVB.NET:处理多个下拉列表
【发布时间】:2014-03-06 16:55:51
【问题描述】:

每当我必须处理许多下拉列表 (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 应用程序

提前致谢。

【问题讨论】:

  • 谷歌这个:asp.net cascading dropdownlist
  • CascadingDropDown 很酷,但我的要求是我有多个 ParentControl。具体来说,我有 6 个 ddl,其中 5 个是相互依赖的,最重要的是我必须给用户 [All] 选项。是否有任何行业最佳实践。来吧!通常我们在招聘网站上看到有很多 ddl 并且每个都有 [All] 选项。他们如何根据其他 ddl 的选择填充每个 ddl。请任何该领域的专家..

标签: asp.net vb.net drop-down-menu


【解决方案1】:

好吧,我使用一个自定义类解决了它,可能存在更好的方法,但我们开始

规则

 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
 ...

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多