2013-07-22 49 views
1

我有很多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(); 
    } 

會有人對此有所幫助.. 非常感謝提前...

+7

使用[switch](http://msdn.microsoft.com/zh-cn/library/06tc147t(v = vs.71).aspx)語句? – Jordy

+1

如何使用switch e – user1

回答

10

呃?

它可以除非你想一些檢查(該ddlBr1Type是有效的)清楚地簡化爲

{ 
    ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString()); 
    ddlBr2Type.DataBind(); 
} 

在這種情況下

{ 
    var valid = new HashSet<string>{ ValidationFields.FW.ToString(), ..... }; 
    if (valid.Contains(ddlBr1Type.SelectedValue)) 
    { 
     ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString()); 
     ddlBr2Type.DataBind(); 
    } 
} 
+0

很多謝謝....我正在尋找這一個.... –

+0

等待4分鐘以上打勾:) –

1

如果我沒有誤解你的代碼,這應該這樣做:

protected void ddlBr1Type_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString()); 
    ddlBr2Type.DataBind(); 
} 
+0

很多感謝這個答案.. –

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語句消失。

+0

很多感謝這個答案.. –

1
ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString()); 
ddlBr2Type.DataBind(); 
+0

很多感謝您的回答.. –

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>(); 
    } 
} 
+0

很多感謝這個答案.. –

0

你可以有使用的開關。但是,因爲每一次你重複的比較正確的面值,並假設它不會改變,我會簡單地重寫這樣第一種方法:

ddlBr2Type.DataSource = populateNearByDropdown(ddlBr1Type.SelectedValue.ToString()); 
ddlBr2Type.DataBind(); 

至於第二個,我還以爲的馬桶蓋/箱,或者一些反映這種類型的值(ValidationTypes。[信]的ToString(),但它不是很穩定,所以我不知道。