我在下拉列表中有很多 if-else 语句选择更改事件,用于根据当前下拉列表中的值选择填充另一个下拉列表。有没有通用的方法来减少任何参数类型的 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();
}
有人可以帮忙吗.. 非常感谢提前...