欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net Gridview分页保存选项

程序员文章站 2024-02-28 15:43:46
复制代码 代码如下:#region //'revision: 1.00 created date: 2013/08/02 created id: una [#1300071...

复制代码 代码如下:

#region //'revision: 1.00 created date: 2013/08/02 created id: una [#1300071]增加多選框
        /// <summary>
        /// session獲取多選框值
        /// </summary>
        private void rememberoldvalues()
        {
            arraylist categoryidlist = new arraylist();
            string index = "";
            foreach (gridviewrow row in gridview.rows)
            {
                index = (string)gridview.datakeys[row.rowindex].value;
                bool result = ((checkbox)row.findcontrol("deletethis")).checked;

                // check in the session
                if (session["id"] != null)
                    categoryidlist = (arraylist)session["id"];
                if (result)
                {
                    if (!categoryidlist.contains(index))
                        categoryidlist.add(index);
                }
                else
                    categoryidlist.remove(index);
            }
            if (categoryidlist != null && categoryidlist.count > 0)
                session["id"] = categoryidlist;
        }

        /// <summary>
        /// session分頁時之前多選框為true
        /// </summary>
        private void repopulatevalues()
        {
            arraylist categoryidlist = (arraylist)session["id"];
            if (categoryidlist != null && categoryidlist.count > 0)
            {
                foreach (gridviewrow row in gridview.rows)
                {
                    string index = (string)gridview.datakeys[row.rowindex].value;
                    if (categoryidlist.contains(index))
                    {
                        checkbox mycheckbox = (checkbox)row.findcontrol("deletethis");
                        mycheckbox.checked = true;
                    }
                }
            }
        }
        #endregion


复制代码 代码如下:

protected void gridview_pageindexchanging(object sender, gridviewpageeventargs e)
        {
            rememberoldvalues();
            gridview.pageindex = e.newpageindex;
            binddata();
            repopulatevalues();
        }

复制代码 代码如下:

protected void btnselect_click(object sender, eventargs e)
        {
            string items = "";
            arraylist categoryidlist = new arraylist();
            string index ="";
            foreach (gridviewrow row in gridview.rows)
            {
                index = (string)gridview.datakeys[row.rowindex].value;
                bool result = ((checkbox)row.findcontrol("deletethis")).checked;

                // check in the session
                if (session["id"] != null)
                    categoryidlist = (arraylist)session["id"];
                if (result)
                {
                    if (!categoryidlist.contains(index))
                        categoryidlist.add(index);
                }
                else
                    categoryidlist.remove(index);
            }
            if (categoryidlist != null && categoryidlist.count > 0)
                for (int i = 0; i < categoryidlist.count; i++)
                {
                    items += categoryidlist[i] + ",";
                }
            items = items.substring(0, items.length - 1);
            scriptmanager.registerstartupscript(this, this.gettype(), "", "check('" + items + "');", true);
            session.remove("id");
        }