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++;
}
}
}
ListBox和DropDownList遍历控件获取SelectedIndex属性
最新推荐文章于 2022-10-02 16:11:14 发布