我有很多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是一個enumlists代碼,這是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();
}
會有人對此有所幫助.. 非常感謝提前...
使用[switch](http://msdn.microsoft.com/zh-cn/library/06tc147t(v = vs.71).aspx)語句? – Jordy
如何使用switch e – user1