获取CheckBoxList选中项的值及设置选中项

返回CheckBoxList选中项的值

#region 返回选中项的值(CheckBoxList)

/// <summary>

/// 返回选中项的值(CheckBoxList)

/// </summary>

/// <param name="rbl">CheckBoxList控件id</param>

/// <param name="splitChar">分隔符</param>

/// <returns></returns>

public static string GetSelectedValue(CheckBoxList cbl, string splitChar)

{

    //声明返回值变量

    string returnValue = "";

 

    //遍历控件

    for (int i = 0; i < cbl.Items.Count; i++)

    {

        if (cbl.Items[i].Selected == true)

        {

            if (returnValue != "")

                returnValue += splitChar;

 

            returnValue += cbl.Items[i].Text.Trim();

        }

    }

 

    //返回值

    return returnValue;

}

#endregion

 

设置CheckBoxList的选中项

#region 设置选中项(CheckBoxList)

/// <summary>

/// 设置选中项(CheckBoxList)

/// </summary>

/// <param name="cbl">CheckBoxList控件id</param>

/// <param name="splitChar">分隔符</param>

/// <param name="value">要设置选中项的值</param>

public static void SetSelectedItemByValue(CheckBoxList cbl, char[] splitChar, string value)

{

    //构造值的数组形式

    string[] arrValue = value.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);

 

    //遍历控件

    for (int i = 0; i < arrValue.Length; i++)

    {

        for (int j = 0; j < cbl.Items.Count; j++)

        {

            if (arrValue[i].Trim() == cbl.Items[j].Text.Trim())

            {

                cbl.Items[j].Selected = true;

            }

        }

    }

}

#endregion

来源: 谢斌个人博客获取CheckBoxList选中项的值及设置选中项
转载请以链接形式标明本文地址!本文地址:https://www.xb02.com/article/28
发表评论

发表评论