用数组作checkboxlist数据源

 

public partial class User_Expectations : System.Web.UI.Page
{
    Guid _rubricId;
    Guid _courseId;
    Guid _gradeId;
    string _str;
    protected void Page_Load(object sender, EventArgs e)
    {
        _rubricId = new Guid(Request.QueryString["Rubric_id"].ToString());
        Rubric.BLL.Rubrics myBll = new Rubric.BLL.Rubrics();
        _courseId = new Guid(myBll.GetModel(_rubricId).CourseId.ToString());
        _gradeId = new Guid(myBll.GetModel(_rubricId).GradeId.ToString());
        if (!Page.IsPostBack)
        {
            CheckBoxList1Bind();
            CheckBoxList2Bind();
        }
    }
    protected void rbl_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    #region datalist绑定方法
    //private void dlDescription1Bind()
    //{
    //    Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();

    //    if (rbl.Items[0].Selected)
    //    {
    //        _str = "CourseId='" + _courseId + "' and IsSpecific=0";
    //        DataSet ds = myBll.GetList(_str);
    //        dlDescription1.DataSource = ds;
    //        dlDescription1.DataBind();
    //    }
    //    else
    //    {
    //        _str = "GradeId='" + _gradeId + "' and IsSpecific=0";
    //        DataSet ds = myBll.GetList(_str);
    //        dlDescription1.DataSource = ds;
    //        dlDescription1.DataBind();
    //    }

    //} 
    #endregion

    #region CheckBoxList1数据绑定
    private void CheckBoxList1Bind()
    {
        Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();

        if (rbl.Items[0].Selected)
        {
            _str = "CourseId='" + _courseId + "' and IsSpecific=0";
            DataSet ds = myBll.GetList(_str);
            CheckBoxList1.DataSource = ds;
            CheckBoxList1.DataTextField = "Description";
            CheckBoxList1.DataValueField = "ExpecId";
            CheckBoxList1.DataBind();
        }
        else
        {
            _str = "GradeId='" + _gradeId + "' and IsSpecific=0";
            DataSet ds = myBll.GetList(_str);
            CheckBoxList1.DataSource = ds;
            CheckBoxList1.DataTextField = "Description";
            CheckBoxList1.DataValueField = "ExpecId";
            CheckBoxList1.DataBind();
        }

    }
    #endregion

    #region CheckBoxList2数据绑定
    private void CheckBoxList2Bind()
    {
        Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();

        if (rbl.Items[0].Selected)
        {
            _str = "CourseId='" + _courseId + "' and IsSpecific=1";
            DataSet ds = myBll.GetList(_str);
            CheckBoxList2.DataSource = ds;
            CheckBoxList2.DataTextField = "Description";
            CheckBoxList2.DataValueField = "ExpecId";
            CheckBoxList2.DataBind();
        }
        else
        {
            _str = "GradeId='" + _gradeId + "' and IsSpecific=1";
            DataSet ds = myBll.GetList(_str);
            CheckBoxList2.DataSource = ds;
            CheckBoxList2.DataTextField = "Description";
            CheckBoxList2.DataValueField = "ExpecId";
            CheckBoxList2.DataBind();
        }

    }
    #endregion

    #region datalist数据绑定
    //private void dlDescription2Bind()
    //{
    //    Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();

    //    if (rbl.Items[0].Selected)
    //    {
    //        _str = "CourseId='" + _courseId + "' and IsSpecific=1";
    //        DataSet ds = myBll.GetList(_str);
    //        dlDescription2.DataSource = ds;
    //        dlDescription2.DataBind();
    //    }
    //    else
    //    {
    //        _str = "GradeId='" + _gradeId + "' and IsSpecific=1";
    //        DataSet ds = myBll.GetList(_str);
    //        dlDescription2.DataSource = ds;
    //        dlDescription2.DataBind();
    //    }

    //} 
    #endregion
    #region 测试用的
    添加选中项1
    //protected void btnAdd1_Click(object sender, EventArgs e)
    //{

    //    #region 没有用的
    //    //for (int i = 0; i < dlDescription1.Items.Count; i++)
    //    //{
    //    //    if (dlDescription1.Items[i].ItemType == ListItemType.SelectedItem)
    //    //    {
    //    //        CheckBox chk = (CheckBox)dlDescription1.Items[i].FindControl("chkItem");
    //    //        if (chk.Checked)
    //    //        {
    //    //            //Guid _expecId = new Guid(dlDescription1.DataKeys[i].ToString());
    //    //            //Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();

    //    //            //DataSet ds = myBll.GetList("ExpecId='" + _expecId + "'");
    //    //            //dlSelected1.DataSource = ds;
    //    //            //dlSelected1.DataBind();
    //    //            Response.Write(chk.Text);
    //    //        }
    //    //    }

    //    //} 
    //    #endregion

    //    foreach (DataListItem myItem in dlDescription1.Items)
    //    {
    //        CheckBox chk = (CheckBox)myItem.FindControl("chkItem");
    //        if (chk.Checked)
    //        {
    //            Guid _expecId = new Guid(dlDescription1.DataKeys[myItem.ItemIndex].ToString());
    //            Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();

    //            DataSet ds = myBll.GetList("ExpecId='" + _expecId + "'");
    //            dlSelected1.DataSource = ds;

    //            Response.Write(chk.Text);
    //        }
    //    }
    //    dlSelected1.DataBind();
    //} 
    #endregion
    ///     /// 将一般期望左边的添加到右边
    /// 
    #region 将左边的添加到右边
    //protected void Button1_Click(object sender, EventArgs e)
    //{
    //    //for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    //    //{
    //    //    if (CheckBoxList1.Items[i].Selected)
    //    //    {
    //    //        Response.Write(CheckBoxList1.Items[i].Text.ToString());
    //    //        Guid _expecId = new Guid(CheckBoxList1.Items[i].Value.ToString());
    //    //        Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();
    //    //        ListItem item = new ListItem();
    //    //        item.Text = myBll.GetModel(_expecId).Description.ToString(); ;
    //    //        item.Value = myBll.GetModel(_expecId).ExpecId.ToString();
    //    //        CheckBoxList2.Items.Add(item);
    //    //        CheckBoxList1.Items.Remove(item);
    //    //    }
    //    //}
    //}
    #endregion
    protected void btnAdd1_Click(object sender, EventArgs e)
    {
        int _count=int.Parse(CheckBoxList1.Items.Count.ToString());
        string[] arr=new string[_count];

        for (int i = 0; i < arr.Length;i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                Guid _expecId = new Guid(CheckBoxList1.Items[i].Value.ToString());
                Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();
                arr[i] = myBll.GetModel(_expecId).Description.ToString();
               
                CheckBoxList1.Items[i].Selected = false;
                CheckBoxList12.DataValueField = myBll.GetModel(_expecId).ExpecId.ToString();
                CheckBoxList12.Items.Add(arr[i].ToString());
                //CheckBoxList1.Items.RemoveAt(i);//从集合中移除指定索引的listitem
              //  CheckBoxList1.DataBind();
              //  CheckBoxList12.DataBind();
            }
        }
        #region 测试代码
        //foreach (ListItem item in CheckBoxList1.Items)
        //{
        //    if (item.Selected)
        //    {
        //        Guid _expecId = new Guid(item.Value.ToString());
        //        Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();
        //        item.Text = myBll.GetModel(_expecId).Description.ToString(); ;
        //        item.Value = myBll.GetModel(_expecId).ExpecId.ToString(); 
        //        CheckBoxList2.Items.Add(item);
        //        CheckBoxList1.Items.Remove(item);
        //    }

        //} 
        #endregion
        
    }
    //添加特殊期望到右边
    protected void btnAdd2_Click(object sender, EventArgs e)
    {
        int _count = int.Parse(CheckBoxList2.Items.Count.ToString());
        string[] arr = new string[_count];

        for (int i = 0; i < arr.Length; i++)
        {
            if (CheckBoxList2.Items[i].Selected)
            {
                Guid _expecId = new Guid(CheckBoxList2.Items[i].Value.ToString());
                Rubric.BLL.Expectations myBll = new Rubric.BLL.Expectations();
                arr[i] = myBll.GetModel(_expecId).Description.ToString();
                
                CheckBoxList2.Items[i].Selected = false;
                CheckBoxList22.DataValueField = myBll.GetModel(_expecId).ExpecId.ToString();
                CheckBoxList22.Items.Add(arr[i].ToString());
              //  CheckBoxList2.Items.RemoveAt(i);//从集合中移除指定索引的listitem
            }
        }
    }
    //移除选定的一般期望到左边
    protected void btnRemove1_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < CheckBoxList22.Items.Count; i++)
            {
                if (CheckBoxList12.Items[i].Selected)
                {

                    CheckBoxList12.Items.RemoveAt(i);
                    CheckBoxList12.DataBind();
                }
            }
        }
        catch
        {
            
        }
    }
    //移除选定的特殊期望到左边
    protected void btnRemove2_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < CheckBoxList22.Items.Count; i++)
            {
                if (CheckBoxList22.Items[i].Selected)
                {
                    CheckBoxList22.Items.RemoveAt(i);
                    CheckBoxList22.DataBind();
                }
            }
        }
        catch
        {

        }
        
    }
    //移除所有选中的一般期望
    protected void btnRemoveAll1_Click(object sender, EventArgs e)
    {
        CheckBoxList12.Items.Clear();
    }
    //移除所有选中的特殊期望
    protected void btnRemoveAll2_Click(object sender, EventArgs e)
    {
        CheckBoxList22.Items.Clear();
    }
    //
    protected void btnAdd_Click(object sender, EventArgs e)
    {

    }
}

 

转载于:https://www.cnblogs.com/jianfangkk/archive/2008/06/25/1367189.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值