码迷,mamicode.com
首页 > Web开发 > 详细

asp.net 翻页时用ViewState保存上一页checkbox勾选的值

时间:2014-07-03 09:33:08      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:os   for   .net   type   text   ar   

 /// <summary>
        /// checkbox勾选取消勾选事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void checkboxl_OnCheckedChanged(Object sender, EventArgs e)
        {
            string tempValue = string.Empty;//拼接联合主键的临时变量
            var checkbox = sender as CheckBox;
            GridViewRow row = (GridViewRow)checkbox.NamingContainer;//获取行属性
            Label lbSNo = (Label)row.FindControl("SNo");
            Label lbItemNo = (Label)row.FindControl("ItemNo");

            if (checkbox.Checked)//勾选
            {
                ViewState["retuvalue"] += lbSNo.Text.ToString() + "," + lbItemNo.Text + ";";
            }
            else//取消勾选
            {
                //当取消勾选时,将ViewState["retuvalue"]转成集合,去移除ViewState["retuvalue"]中的项
                List<string> templist = ViewState["retuvalue"].ToString().TrimEnd(‘;‘).Split(‘;‘).ToList();
                for (int i = 0; i < templist.Count; i++)
                {
                    if (templist[i].ToString() == lbSNo.Text.ToString() + "," + lbItemNo.Text)
                    {
                        templist.Remove(templist[i]);
                        i--;
                        continue;
                    }
                        tempValue += templist[i] + ";";//再次拼接
                }
                ViewState["retuvalue"] = tempValue;

            }
        }

 

 

  //航绑定事件
        protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (ViewState["retuvalue"] != null)
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox check = (CheckBox)e.Row.Cells[10].FindControl("checkboxl");
                    //拿到两个主键拼接到string中
                    Label lbSNo = (Label)e.Row.FindControl("SNo");
                    Label lbItemNo = (Label)e.Row.FindControl("ItemNo");
                    if (ViewState["retuvalue"].ToString().TrimEnd(‘;‘).Contains(lbSNo.Text.ToString() + "," + lbItemNo.Text))
                    {
                        check.Checked = true;
                    }
                }
        }

 

asp.net 翻页时用ViewState保存上一页checkbox勾选的值,布布扣,bubuko.com

asp.net 翻页时用ViewState保存上一页checkbox勾选的值

标签:os   for   .net   type   text   ar   

原文地址:http://www.cnblogs.com/fighting2014/p/3818274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!