遍历 DropDownList ListBox

      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++;
          }
      }

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页