GridView翻页记住选中的checkbox

来源:互联网 发布:西安智汇诚网络怎么样 编辑:程序博客网 时间:2024/05/18 02:02
 

aspx

 

js或者Juqery照样用,不影响

<%@ Page Title="分配试卷" Language="C#" MasterPageFile="~/Teacher/Teacher.Master"
    CodeBehind="TestArrangePapers.aspx.cs" Inherits="OnlineTest.Teacher.TestArrangePapers" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <link href="../css/Utitlity.css" rel="stylesheet" type="text/css" />
    <script src="../Scripts/Utility.js" type="text/javascript"></script>
    <style>
        .lst
        {
            width: 250px;
            height: 150px;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {

            $('#checkAll').click(function () {
                // 全选或者取消全选
                $('input[type=checkbox]').attr("checked", $(this).attr("checked"));
            });

            // 触发判断是否全选
            $("input[type=checkbox]:not('#checkAll')").click(function () {
                JudgeIsCheckedAll();
            });

            // 检测是否要勾选"全选"
            function JudgeIsCheckedAll() {
                var flag = true;
                $("input[type=checkbox]:not('#checkAll')").each(function () {
                    if ($(this).attr("checked") == false) {
                        flag = false;
                        return false;
                    }
                    else {
                        flag = true;
                    }
                });

                if (flag) {
                    $('#checkAll').attr("checked", true);
                }
                else {
                    $('#checkAll').attr("checked", false);
                }
            }

            // 下一步
            $('#ContentPlaceHolder1_imgBtnImage').click(function () {
                var papers = "";
                $("input[type=checkbox]:not('#checkAll')").each(function () {
                    if ("" != $(this).val()) {
                        papers += $(this).next("input[type=hidden]").val() + ",";
                    }
                    else {
                        alert("您所填的信息不完整");
                        return false;
                    }
                });
                $('#ContentPlaceHolder1_hiddenPapers').val(papers);
            });
        });
    </script>
    <style type="text/css">
        .tdBlue
        {
            background-image: url('../images/footbk.png');
            background-repeat: repeat-x;
        }
    </style>
    <table width="730" class="tablestyle">
        <asp:HiddenField ID="hiddenClassIds" runat="server" />
        <asp:HiddenField ID="hiddenPapers" runat="server" />
        <tr>
            <td style="background-image: url('../images/footbk.png'); background-repeat: repeat-x;
                color: White; font-weight: bold; height: 46px;">
                <div align="center">
                    可选的试卷</div>
            </td>
        </tr>
        <tr>
            <td align="center" style="padding-bottom: 20px; padding-top: 20px">
                <br />
                <asp:GridView ID="GridView1" runat="server" DataKeyNames="id" AllowSorting="true"
                    CellPadding="5" GridLines="Both"

                      BorderColor="Black"
                    Width="90%">
                    <HeaderStyle BorderColor="Black" CssClass="tdBlue"

                            Height="30px" Font-Bold="True"
                        HorizontalAlign="Center" />
                    <RowStyle BorderColor="Black" Height="20px"

                            HorizontalAlign="Center" Font-Size="Small" />
                    <Columns>
                        <asp:TemplateField ItemStyle-Width="35px">
                            <HeaderTemplate>
                                <input id="checkAll" type="checkbox" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="chk" runat="server"/>
                                <input type="hidden" value='<%#eval_r("ID")%>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="序号" ItemStyle-Width="65px">
                            <ItemTemplate>
                                <%# Container.DataItemIndex + 1 %>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="试卷名称">
                            <ItemTemplate>
                                <%#eval_r("Name")%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="课程名称">
                            <ItemTemplate>
                                <%#GetLesson(eval_r("LessonNum")).Name%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="出卷时间">
                            <ItemTemplate>
                                <%#eval_r("PaperTime") %>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="总分" ItemStyle-Width="65px">
                            <ItemTemplate>
                                <%#eval_r("Marks") %>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="审核状态" ItemStyle-Width="105px">
                            <ItemTemplate>
                                已审核
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>
                        <table cellspacing="0" cellpadding="5" rules="all"

                              style="width: 100%; border: 0;
                            border-collapse: collapse">
                            <tr class="tdBlue" align="center"

                                  style="font-weight: bold; height: 30px;">
                                <th scope="col">
                                    序号
                                </th>
                                <th scope="col">
                                    试卷名称
                                </th>
                                <th scope="col">
                                    课程名称
                                </th>
                                <th scope="col">
                                    出卷时间
                                </th>
                                <th scope="col">
                                    总分
                                </th>
                                <th scope="col">
                                    审核状态
                                </th>
                                <th scope="col">
                                    编辑
                                </th>
                                <th scope="col">
                                    删除
                                </th>
                            </tr>
                            <tr align="center" style="font-size: Small; height: 30px;">
                                <td colspan="8">
                                    暂无数据
                                </td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>
                </asp:GridView>
                <br />
                <span>
                    <webdiyer:AspNetPager CssClass="paginator"                               

                     CurrentPageButtonClass="cpb" PageSize="10"                     

                           runat="server" ID="AspNetPager1"

                             FirstPageText="首页" NumericButtonCount="5" LastPageText="尾页"
                        MoreButtonType="Image" NextPageText="下一页"

                      NumericButtonType="Image" OnPageChanging="AspNetPager1_PageChanging"
                        HorizontalAlign="Center" PageIndexBoxType="DropDownList"

                               PrevPageText="上一页" ShowBoxThreshold="0"
                        ShowNavigationToolTip="True">
                    </webdiyer:AspNetPager>
                </span><span style="float: right; position: relative; right: 30px; padding: 20px">
                    <asp:ImageButton ID="imgBtnImage" runat="server"

                       ImageUrl="../images/next2.gif" OnClick="imgBtnImage_Click" />
                </span>
            </td>
        </tr>
    </table>
</asp:Content>

 

 

cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tool;
using OnlineTest.Datalinq;
using System.Collections;

namespace OnlineTest.Teacher
{
    public partial class TestArrangePapers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (null != Request.QueryString["strClassNums"]
                    && !"".Equals(Request.QueryString["strClassNums"]))
                {
                    // 班级编号
                    this.hiddenClassIds.Value = Request.QueryString["strClassNums"];
                }
                Bind();
            }
        }

        protected void imgBtnImage_Click(object sender, ImageClickEventArgs e)
        {
            CompareOld();
            String strPaperNum = "";
            ArrayList lstIds = new ArrayList();
            if (null != Session["chkItems"])
            {
                lstIds = (ArrayList)Session["chkItems"];
                foreach (var v in lstIds)
                {
                    strPaperNum += v.ToString() + ",";
                }
            }
            else
            {
                // 没有分页的话,直接拿当前页面即可
                strPaperNum = this.hiddenPapers.Value;
            }

            if (null != strPaperNum && !"".Equals(strPaperNum)
                && null != this.hiddenClassIds.Value
                && !"".Equals(this.hiddenClassIds.Value))
            {
                Server.Transfer("TestArrangeTime.aspx?strPaperNum="

                        + strPaperNum + "&strClassNums=" + this.hiddenClassIds.Value);
            }
        }

        public M_Lesson GetLesson(Object lessonNum)
        {
            return BLL.M_LessonBLL.ReadModel(lessonNum.ToString());
        }


        // 绑定已经审核的试卷
        public void Bind()
        {
            if (null != BLL.M_Test_PapeBLL.ReadModels(Utility.GetUserFromSession(), 1))
            {
                IEnumerable<M_Test_Paper> lst =

                            BLL.M_Test_PapeBLL.ReadModels(Utility.GetUserFromSession(), 1);

                // 起始条数
                int startRecord = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1);

                // 每页数目
                int maxRecords = AspNetPager1.PageSize;
                this.AspNetPager1.AlwaysShow = true;

                // 总数目
                this.AspNetPager1.RecordCount = lst.Count();
                this.GridView1.DataSource = lst.Skip(startRecord).Take(maxRecords);
                this.GridView1.DataBind();
            }
        }

        // 分页事件
        protected void AspNetPager1_PageChanging(object src,

                       Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            OldChecked();
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            Bind();
            ShowOldChecked();
        }

        /// <summary>
        /// 记住翻页前的选项
        /// </summary>
        public void OldChecked()
        {
            ArrayList lstIds = new ArrayList();
            int index = -1;
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                index = (int)GridView1.DataKeys[row.RowIndex].Value;
                bool result = ((CheckBox)row.FindControl("chk")).Checked;
                if (null != Session["chkItems"])
                {
                    lstIds = (ArrayList)Session["chkItems"];
                }
                if (result)
                {
                    if (!lstIds.Contains(index))
                    {
                        lstIds.Add(index);
                    }
                    if (null != lstIds && lstIds.Count != 0)
                    {
                        Session["chkItems"] = lstIds;
                    }
                }
            }
        }


        /// <summary>
        /// 返回该页时显示以前选择的选项
        /// </summary>
        public void ShowOldChecked()
        {
            ArrayList lstIds = (ArrayList)Session["chkItems"];
            if (null != lstIds && lstIds.Count != 0)
            {
                foreach (GridViewRow row in this.GridView1.Rows)
                {
                    int index = (int)GridView1.DataKeys[row.RowIndex].Value;
                    if (lstIds.Contains(index))
                    {
                        CheckBox chk = (CheckBox)row.FindControl("chk");
                        chk.Checked = true;
                    }
                }
            }
        }

        /// <summary>
        /// 提交时比较本页的选择项是否有变化
        /// </summary>
        public void CompareOld()
        {
            ArrayList lstIds = new ArrayList();
            int index = -1;
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                if (null != Session["chkItems"])
                {
                    lstIds = (ArrayList)Session["chkItems"];
                    index = (int)GridView1.DataKeys[row.RowIndex].Value;
                    bool result = ((CheckBox)row.FindControl("chk")).Checked;

                    // 如果以前选了,现在不选,则去除
                    if (!result && lstIds.Contains(index))
                    {
                        lstIds.Remove(index);
                    }

                    if (null != lstIds && lstIds.Count != 0)
                    {
                        Session["chkItems"] = lstIds;
                    }
                }
            }
        }
    }
}

原创粉丝点击