zoukankan      html  css  js  c++  java
  • GridView中的CheckBox 翻页时记录选中状态

    思路就是翻页的时候记录一下checkbox的选中状态 可以赋值给viewstate 或者session 都行这里我用的是 viewstate ,记录完了 在根据记录的选择状态 进库 或者是 给checkbox赋值


    //页面加载时给GridView赋值

    string[] B = ds.Tables[0].Rows[0]["KaoPingYuan"].ToString().Split(',');
    ArrayList al1 = new ArrayList(B);
    ViewState["CHECKED_ITEMS"] = al1;
    RePopulateValues();
    

    #region =考评员gridview1相关代码=
    
    
            private void RememberOldValues()
            {
                ArrayList categoryIDList = new ArrayList();
                int index = -1;
                foreach (GridViewRow row in GridView1.Rows)
                {
                    index = (int)GridView1.DataKeys[row.RowIndex].Value;
                    bool result = ((CheckBox)row.FindControl("checkD")).Checked;
    
    
                    if (ViewState["CHECKED_ITEMS"] != null)
                        categoryIDList = (ArrayList)ViewState["CHECKED_ITEMS"];
                    if (result)
                    {
                        if (!categoryIDList.Contains(index))
                            categoryIDList.Add(index);
                    }
                    else
                        categoryIDList.Remove(index);
                }
                if (categoryIDList != null && categoryIDList.Count > 0)
                    ViewState["CHECKED_ITEMS"] = categoryIDList;
            }
    
    
            private void RePopulateValues()
            {
                ArrayList categoryIDList = (ArrayList)ViewState["CHECKED_ITEMS"];
                if (categoryIDList != null && categoryIDList.Count > 0)
                {
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        int index = (int)GridView1.DataKeys[row.RowIndex].Value;
                        if (categoryIDList.Contains(index.ToString()))//这里如果你数据库里面存的是int形式的id 就不用tostring,但是因为是复选 储存形式大部分都是"id1,id2,id3,id4"的字符串这时候就需要把GridView1绑定的int型的id转成string才能判断是否相等。
                        {
                            CheckBox myCheckBox = (CheckBox)row.FindControl("checkD");
                            myCheckBox.Checked = true;
                        }
                    }
                }
            }
    
    
            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                RememberOldValues();
                GridView theGrid = sender as GridView;
                int newPageIndex = 0;
                if (-2 == e.NewPageIndex)
                {
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = theGrid.BottomPagerRow;
                    if (null != pagerRow)
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                    if (null != txtNewPageIndex)
                    {
                        if (Regex.IsMatch(txtNewPageIndex.Text, @"^d+$"))
                            newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                        else
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入数字');</script>");
                    }
                }
                else
                    newPageIndex = e.NewPageIndex;
                newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
                theGrid.PageIndex = newPageIndex;
                LoadGrivdView1();
                RePopulateValues();
            }
    
    
            #endregion
    	protected void LoadGrivdView1()
            {
                DataSet ds = db.db.GetDataSet("select * from tb_KPY");
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
    <pre name="code" class="cpp">protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                RememberOldValues();
                GridView theGrid = sender as GridView;
                int newPageIndex = 0;
                if (-2 == e.NewPageIndex)
                {
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = theGrid.BottomPagerRow;
                    if (null != pagerRow)
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                    if (null != txtNewPageIndex)
                    {
                        if (Regex.IsMatch(txtNewPageIndex.Text, @"^d+$"))
                            newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                        else
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入数字');</script>");
                    }
                }
                else
                    newPageIndex = e.NewPageIndex;
                newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
                theGrid.PageIndex = newPageIndex;
                LoadGrivdView1();
                RePopulateValues();
            }


    
    
    //保存的时候进入数据库

     <span style="white-space:pre">	</span>    RememberOldValues();
                string list = "";
                string id = Guid.NewGuid().ToString();
                ArrayList categoryIDList = new ArrayList();
                categoryIDList = (ArrayList)ViewState["CHECKED_ITEMS"];
                for (int i = 0; i < categoryIDList.Count; i++)
                {
                    list += categoryIDList[i] + ",";
                }
                list.TrimEnd(',');
    然后把list insert into 表就行了


    前台gridview代码

    <asp:GridView ID="GridView1" runat="server" RowStyle-HorizontalAlign="Center"   AutoGenerateColumns="False"  Width="100%" DataKeyNames="Userid"
                    AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" ForeColor="#333333">
                    <Columns>
                        <asp:TemplateField Visible="false">
                            <ItemTemplate>
                                <asp:Label ID="lbuserid" runat="server" Text='<%#Eval("Userid") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="选择">
                            <ItemTemplate>
                                <asp:CheckBox ID="checkD" runat="server" />
                            </ItemTemplate>
                            <ItemStyle BorderColor="#507CD1" HorizontalAlign="Center" BorderWidth="1px" />
                        </asp:TemplateField>
                        
                        <asp:BoundField  HeaderText="姓名" DataField="name"  ItemStyle-BorderColor="#507CD1" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderWidth="1px"/>
                        
                        <asp:BoundField  HeaderText="性别"  DataField="sex" ItemStyle-BorderColor="#507CD1" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderWidth="1px"/>
                        
                        <asp:BoundField  HeaderText="出生年月"  DataField="birthday" ItemStyle-BorderColor="#507CD1" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderWidth="1px"/>
                        
                        <asp:BoundField  HeaderText="专业年限"  DataField="workyear" ItemStyle-BorderColor="#507CD1" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderWidth="1px"/>
                        
                        <asp:BoundField  HeaderText="技术等级"  DataField="level" ItemStyle-BorderColor="#507CD1" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderWidth="1px"/>
                        
                        <asp:BoundField  HeaderText="工作单位"  DataField="company" ItemStyle-BorderColor="#507CD1" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderWidth="1px"/>
                        
                        <asp:BoundField  HeaderText="电话"  DataField="telephone" ItemStyle-BorderColor="#507CD1" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderWidth="1px"/>
                        
                        <asp:BoundField  HeaderText="考评员证号" DataField="sno"  ItemStyle-BorderColor="#507CD1" ItemStyle-HorizontalAlign="Center" ItemStyle-BorderWidth="1px"/>
                        
                    </Columns>
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerTemplate>
                        <table style="font-size: 12px" width="100%">
                            <tbody>
                            <tr>
                               <td style="text-align: center">
                                   当前第:
                                   <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
                                   页/共:
                                   <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
                                   页
                                   <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
                                       ForeColor="#666699" Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
                                   <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
                                       ForeColor="#666699" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
                                   <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
                                       ForeColor="#666699" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
                                   <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
                                       ForeColor="#666699" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
                                   转到第
                                   <asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
                                   <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"
                                       ForeColor="#666699" CommandName="Page" Text="GO" />
                                  </td>
                               </tr>
                             </tbody>
                             </table>
                         </PagerTemplate>
                    </asp:GridView>






  • 相关阅读:
    算法训练 P1103
    算法训练 表达式计算
    算法训练 表达式计算
    基础练习 时间转换
    基础练习 字符串对比
    Codeforces 527D Clique Problem
    Codeforces 527C Glass Carving
    Codeforces 527B Error Correct System
    Codeforces 527A Glass Carving
    Topcoder SRM 655 DIV1 250 CountryGroupHard
  • 原文地址:https://www.cnblogs.com/yanergui/p/5014326.html
Copyright © 2011-2022 走看看