获取RadioButtonList选中项的值及设置选中项
返回RadioButtonList选中项的值
#region 返回选中项的值(RadioButtonList)
/// <summary>
/// 返回选中项的值(RadioButtonList)
/// </summary>
/// <param name="rbl">RadioButtonList控件id</param>
/// <param name="splitChar">分隔符</param>
/// <returns></returns>
public static string GetSelectedValue(RadioButtonList rbl, string splitChar)
{
//声明返回值变量
string returnValue = "";
//遍历控件
for (int i = 0; i < rbl.Items.Count; i++)
{
if (rbl.Items[i].Selected == true)
{
if (returnValue != "")
returnValue += splitChar;
returnValue += rbl.Items[i].Text.Trim();
}
}
//返回值
return returnValue;
}
#endregion
设置RadioButtonList的选中项
#region 设置选中项(RadioButtonList)
/// <summary>
/// 设置选中项(RadioButtonList)
/// </summary>
/// <param name="rbl">RadioButtonList控件id</param>
/// <param name="splitChar">分隔符</param>
/// <param name="value">要设置选中项的值</param>
public static void SetSelectedItemByValue(RadioButtonList rbl, char[] splitChar, string value)
{
//构造值得数组形式
string[] arrValue = value.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
//遍历控件
for (int i = 0; i < arrValue.Length; i++)
{
for (int j = 0; j < rbl.Items.Count; j++)
{
if (arrValue[i].Trim() == rbl.Items[j].Text.Trim())
{
rbl.Items[j].Selected = true;
break;
}
}
}
}
#endregion