• 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;
                        }
                    }
            }

    动态绑定grid 记录


                #region 动态绑定gridview
                gvData.Columns.Clear();//先清空gridview
                gvData.AutoGenerateColumns = false;
                gvData.DataSource = dtData;
                //gvData.DataKeys  = new string[] { strDataKey };
                for (int i = 0; i < dtData.Columns.Count; i++)
                {
                    BoundField bfColumn = new BoundField();//用于动态绑定中为本显示的字段

                    bfColumn.DataField = dtData.Columns[i].ColumnName;//字段
                    bfColumn.SortExpression = dtData.Columns[i].ColumnName;
                    bfColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    bfColumn.HeaderText = dtData.Columns[i].Caption;//绑定列的标题
                    bfColumn.HtmlEncode = false;
                    gvData.Columns.Add(bfColumn);
                }
                gvData.DataBind();
                #endregion

  • 相关阅读:
    使用URLEncoder、URLDecoder进行URL参数的转码与解码
    js中的连等==和全等===
    sass用法
    sass/scss 和 less的区别
    JSON与localStorage的爱恨情仇
    ionic2
    雪碧图
    JavaScript交换两个变量值的七种解决方案
    JavaScript中的[]和{}
    为什么 ++[[]][+[]]+[+[]] = 10?
  • 原文地址:https://www.cnblogs.com/fighting2014/p/3818274.html
Copyright © 2020-2023  润新知