利用枚举为数据源,封装对应数据集合
1、定义枚举
/// <summary>
/// 枚举方法
/// </summary>
public static enum RadioMailState
{
All=0, Men = 1, Women = 2
}
2、需要封装后的共同类
/// <summary>
/// 共同类
/// </summary>
public class Common
{
/// <summary>
/// 获取性别的封装值
/// </summary>
/// <param name="mStrFlag">是否包含全部默认值</param>
/// <returns></returns>
public static Dictionary<RadioMailState, string> getRadioMailState(bool mStrFlag)
{
Dictionary<RadioMailState, string> dStrRadioMailState = new Dictionary<RadioMailState, string>();
if (mStrFlag)
{
dStrRadioMailState.Add(RadioMailState.All, "全部");
}
dStrRadioMailState.Add(RadioMailState.Men, "男");
dStrRadioMailState.Add(RadioMailState.Women, "女");
return dStrRadioMailState;
}
}
3、给调用的下拉控件封装值
/// <summary>
/// 封装对应的按钮
/// </summary>
public class RadioControler
{
/// <summary>
/// 封装性别的下拉框
/// </summary>
/// <param name="listControl">下拉控件</param>
/// <param name="ifCheckAll">是否添加全部默认值</param>
public void LoadRadioMexValue(DropDownList listControl,bool ifCheckAll)
{
Dictionary<RadioMailState, string> dStrMexStateValue = Common.getRadioMailState(ifCheckAll); //声明取值变量
foreach (KeyValuePair<RadioMailState,string> radiovalue in dStrMexStateValue) //循环取值,并赋值给下拉框
{
listControl.Items.Add(new ListItem(radiovalue.Value.ToString(), radiovalue.Key.ToString()));
}
if(listControl.Items.Count>0) //如果选项不为空,那么让第一个默认选中
{
listControl.Items[0].Selected=true;
}
}
}