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

 /// <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勾选的值,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。