public class ControlTools { public static void SetListBoxItem(ListBox listBox,string sItemValue) { int index = 0; foreach(ListItem item in listBox.Items) { ///判断值是否相等,并且设置控件的SelectedIndex if(item.Value.ToLower() == sItemValue.ToLower()) { listBox.SelectedIndex = index; break; } index++; } } public static void SetListBoxItem(DropDownList listBox,string sItemValue) { int index = 0; foreach(ListItem item in listBox.Items) { ///判断值是否相等,并且设置控件的SelectedIndex if(item.Value.ToLower() == sItemValue.ToLower()) { listBox.SelectedIndex = index; break; } index++; } } public static void SetListBoxItem(ListBox listBox,string sItemValue,bool IsBool) { int index = 0; if(IsBool == true) { sItemValue = sItemValue.ToLower() == "true" ? "1" : "0"; } foreach(ListItem item in listBox.Items) { ///判断值是否相等,并且设置控件的SelectedIndex if(item.Value.ToLower() == sItemValue.ToLower()) { listBox.SelectedIndex = index; break; } index++; } } public static void SetListBoxItem(DropDownList listBox,string sItemValue,bool IsBool) { int index = 0; if(IsBool == true) { sItemValue = sItemValue.ToLower() == "true" ? "1" : "0"; } foreach(ListItem item in listBox.Items) { ///判断值是否相等,并且设置控件的SelectedIndex if(item.Value.ToLower() == sItemValue.ToLower()) { listBox.SelectedIndex = index; break; } index++; } } }