我在下拉列表中有很多if-else语句选择了更改事件,用于根据当前下拉列表中的值select来填充另一个下拉列表。 有没有任何通用的方法来减少这么多if-else语句与任何参数类型.. 这是我的下拉列表选择更改事件的代码
protected void ddlBr1Type_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlBr1Type.SelectedValue.ToString() == ValidationFields.FO.ToString())
{
ddlBr2Type.DataSource = populateNearByDropdown(ValidationFields.FO.ToString());
ddlBr2Type.DataBind();
}
else if (ddlBr1Type.SelectedValue.ToString() == ValidationFields.FW.ToString())
{
ddlBr2Type.DataSource = populateNearByDropdown(ValidationFields.FW.ToString());
ddlBr2Type.DataBind();
}
else if (ddlBr1Type.SelectedValue.ToString() == ValidationFields.UF.ToString())
{
ddlBr2Type.DataSource = populateNearByDropdown(ValidationFields.UF.ToString());
ddlBr2Type.DataBind();
}
else if (ddlBr1Type.SelectedValue.ToString() == ValidationTypes.O.ToString())
{
ddlBr2Type.DataSource = populateNearByDropdown(ValidationTypes.O.ToString());
ddlBr2Type.DataBind();
}
else if (ddlBr1Type.SelectedValue.ToString() == ValidationTypes.W.ToString())
{
ddlBr2Type.DataSource = populateNearByDropdown(ValidationTypes.W.ToString());
ddlBr2Type.DataBind();
}
else if (ddlBr1Type.SelectedValue.ToString() == ValidationTypes.P.ToString())
{
ddlBr2Type.DataSource = populateNearByDropdown(ValidationTypes.P.ToString());
ddlBr2Type.DataBind();
}
else if (ddlBr1Type.SelectedValue.ToString() == ValidationTypes.C.ToString())
{
ddlBr2Type.DataSource = populateNearByDropdown(ValidationTypes.C.ToString());
ddlBr2Type.DataBind();
}
}
此处ValidationTypes和validationfields是一个枚举,这是PopulateNearbyDropdown方法的代码
private List<string> populateNearByDropdown(String Item1)
{
var MatchedItems = new List<string>();
var listofitems = new List<string> { "FO", "FW", "O", "W", "UF" };
if (listofitems.Any(str => str.Contains(Item1)))
{
MatchedItems = new List<string> { Item1, ValidationTypes.P.ToString(), ValidationTypes.C.ToString() };
}
else if (Item1 == "P")
{
MatchedItems = new List<string> { Item1, ValidationTypes.C.ToString() };
}
else if (Item1 == "C")
{
MatchedItems = new List<string> { Item1, ValidationTypes.U.ToString() };
}
else if (Item1 == "U" || Item1 == "FC")
{
MatchedItems = new List<string> { Item1 };
}
return MatchedItems.ToList();
}
任何人都会对此有所帮助.. 非常感谢提前...
答案 0 :(得分:10)
嗯?
可以明确简化为
{
ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString());
ddlBr2Type.DataBind();
}
除非你想要一些检查(ddlBr1Type有效)。
在那种情况下
{
var valid = new HashSet<string>{ ValidationFields.FW.ToString(), ..... };
if (valid.Contains(ddlBr1Type.SelectedValue))
{
ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString());
ddlBr2Type.DataBind();
}
}
答案 1 :(得分:2)
由于你依赖字符串相等,我认为你根本不需要任何逻辑。这个if语句:
if (ddlBr1Type.SelectedValue.ToString() == ValidationFields.FO.ToString())
{
ddlBr2Type.DataSource = populateNearByDropdown(ValidationFields.FO.ToString());
ddlBr2Type.DataBind();
}
可以崩溃到:
ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString());
ddlBr2Type.DataBind();
所有其他if语句都会消失。
答案 2 :(得分:2)
你可以这样做:
ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString());
ddlBr2Type.DataBind();
...
private List<string> populateNearByDropdown(String Item1)
{
switch(Item1)
{
case "FO":
case "FW":
case "O":
case "W":
case "UF":
return new List<string> { Item1, ValidationTypes.P.ToString(), ValidationTypes.C.ToString() };
case "P":
return new List<string> { Item1, ValidationTypes.C.ToString() };
case "C":
return new List<string> { Item1, ValidationTypes.U.ToString() };
case "U":
case "FC":
return new List<string> { Item1 };
default:
return new List<string>();
}
}
答案 3 :(得分:1)
如果我不误读您的代码,应该这样做:
protected void ddlBr1Type_SelectedIndexChanged(object sender, EventArgs e)
{
ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString());
ddlBr2Type.DataBind();
}
答案 4 :(得分:1)
ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString());
ddlBr2Type.DataBind();
答案 5 :(得分:0)
您可以使用开关。但是,既然你每次重复比较的正确标准,并假设它不会改变,我只需重写第一种方法:
ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString());
ddlBr2Type.DataBind();
至于第二个,我会想到一个swtich / case,或者可能是这种类型的值的一些反射(ValidationTypes。[letter] .ToString(),但它不是很稳定所以我不确定
答案 6 :(得分:0)
我想是代表 检查this出